<?php
/*
  Categories Functions
*/

// Check to ensure this file is included in osConcert!
defined('_FEXEC') or die();

function format_heading_date($heading_date) {
    // Check if the input is empty or invalid
    if (empty($heading_date) || !strtotime($heading_date)) {
        return ''; // Return a graceful message
    }

    if (SHOW_EVENTS_DATE_FORMAT == 'yes') {
        switch (EVENTS_DATE_FORMAT) {
            case 'd-m-Y':
                return date("l, jS F", strtotime($heading_date)); // Thursday, 1st July
            case 'm-d-Y':
                return date("l, F jS", strtotime($heading_date)); // Thursday, July 1st
            case 'Y-m-d':
                return date("Y, jS F", strtotime($heading_date)); // 2021, 1st July
        }
    } else {
        // Use format from language file
        return strftime(OSCONCERT_FORMAT, strtotime($heading_date));
    }
}

function format_heading_time($heading_time) {
    // Check if the input is empty or invalid
    if (empty($heading_time) || !strtotime($heading_time)) {
        return ''; // Return a graceful message
    }

    // Format the time
    $time = strtotime($heading_time);
    return (TIME_FORMAT == 12) ? date('g:i a', $time) : date('H:i', $time); // 12-hour or 24-hour format
}

function tep_get_products_category_id($products_id) {
    global $FSESSION;
    $the_products_category_query = tep_db_query("SELECT categories_id FROM " . TABLE_PRODUCTS_TO_CATEGORIES . " WHERE products_id = '" . tep_db_input($products_id) . "'");
    $the_products_category = tep_db_fetch_array($the_products_category_query);
    return $the_products_category['categories_id'];
}
function getProductCategoryDetails($product_id) {
    // Get the category ID for the product
    $products_category_id = tep_get_products_category_id($product_id);
    if (!$products_category_id) {
        return array('', '', '', '');
    }

    // Get the top-level category ID and related names
    $top_category_id   = tep_get_top_category_id($products_category_id);
    $top_category_name = tep_get_categories_name($top_category_id);
    $sub_category_name = tep_get_categories_name($products_category_id);

    // Get the parent id and plan id for logic decisions
    $parent_id = tep_get_parent_id($products_category_id);
    $plan_id   = tep_get_plan_id($products_category_id);

    // Determine date and time based on plan_id
    if ($plan_id == 3) {
        // Use sub-category details
        $concert_date = tep_get_categories_date($products_category_id);
        $concert_time = tep_get_categories_time($products_category_id);
    } elseif ($plan_id == 2) {
        // Use parent's details
        $concert_date = tep_get_categories_date($parent_id);
        $concert_time = tep_get_categories_time($parent_id);
    } else {
        // Default: Use the product's category
        $concert_date = tep_get_categories_date($products_category_id);
        $concert_time = tep_get_categories_time($products_category_id);
    }

    // Format date and time
    $formatted_date = format_heading_date($concert_date);
    $formatted_time = format_heading_time($concert_time);

    // Determine heading name and venue:
    if ($plan_id == 3) {
        $heading_name = $top_category_name . ', ' . $sub_category_name;
        $heading_venue = tep_get_categories_venue($products_category_id);
        // Fallback if sub-category venue is empty
        if (empty($heading_venue)) {
            $heading_venue = tep_get_categories_venue($top_category_id);
        }
    } elseif ($plan_id == 2) {
        $heading_name = $top_category_name;
        $heading_venue = tep_get_categories_venue($parent_id);
        if (empty($heading_venue)) {
            $heading_venue = tep_get_categories_venue($top_category_id);
        }
    } else {
        $heading_name = $top_category_name;
        $heading_venue = tep_get_categories_venue($top_category_id);
    }

    return array($heading_name, $heading_venue, $formatted_date, $formatted_time);
}
################################################################################
//where does this $category_id come from?
function get_category_datetime($category_id) {
    global $FSESSION;

    // Initialize variables
    $formatted_date = '';
    $formatted_time = '';

    // Fetch the category details
    $query = tep_db_query("SELECT concert_date, concert_time, parent_id 
                           FROM " . TABLE_CATEGORIES . " c
                           JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd 
                             ON c.categories_id = cd.categories_id
                           WHERE c.categories_id = '" . tep_db_input($category_id) . "' 
                             AND cd.language_id = '" . (int)$FSESSION->languages_id . "'");
    if ($row = tep_db_fetch_array($query)) {
        $concert_date = $row['concert_date'];
        $concert_time = $row['concert_time'];
        $parent_id = $row['parent_id'];

        // If the current category has no date/time, check its parent category
        if ((empty($concert_date) || !strtotime($concert_date)) && $parent_id > 0) {
            return get_category_datetime($parent_id); // Recursively check the parent
        }

        // Format the date and time if they are valid
        if (!empty($concert_date) && strtotime($concert_date)) {
            $formatted_date = format_heading_date($concert_date);
        }
        if (!empty($concert_time) && strtotime($concert_time)) {
            $formatted_time = format_heading_time($concert_time);
        }
    }

    return array($formatted_date, $formatted_time);
}


function tep_get_parent_id($categories_id) {
    global $FSESSION;
    $the_parent_id_query = tep_db_query("SELECT parent_id FROM " . TABLE_CATEGORIES . " WHERE categories_id = '" . tep_db_input($categories_id) . "'");
    $the_parent_id = tep_db_fetch_array($the_parent_id_query);
    return $the_parent_id['parent_id'];
}

function tep_get_top_category_id($categories_id) {
    $current_category_id = $categories_id;
    while (true) {
        $parent_id = tep_get_parent_id($current_category_id);
        if ($parent_id == 0) {
            return $current_category_id; // Found the top-level category
        }
        $current_category_id = $parent_id;
    }
}

function tep_get_categories_name($categories_id) {
    global $FSESSION;
    $the_categories_name_query = tep_db_query("SELECT categories_name FROM " . TABLE_CATEGORIES_DESCRIPTION . " WHERE categories_id = '" . tep_db_input($categories_id) . "' AND language_id = '" . (int)$FSESSION->languages_id . "'");
    $the_categories_name = tep_db_fetch_array($the_categories_name_query);
    return $the_categories_name['categories_name'];
}

function tep_get_categories_venue($categories_id) {
    global $FSESSION;
    $the_categories_venue_query = tep_db_query("SELECT concert_venue FROM " . TABLE_CATEGORIES_DESCRIPTION . " WHERE categories_id = '" . tep_db_input($categories_id) . "' AND language_id = '" . (int)$FSESSION->languages_id . "'");
    $the_categories_venue = tep_db_fetch_array($the_categories_venue_query);
    return $the_categories_venue['concert_venue'];
}

function tep_get_categories_date($categories_id) {
    global $FSESSION;
    $the_categories_date_query = tep_db_query("SELECT concert_date FROM " . TABLE_CATEGORIES_DESCRIPTION . " WHERE categories_id = '" . tep_db_input($categories_id) . "' AND language_id = '" . (int)$FSESSION->languages_id . "'");
    $the_categories_date = tep_db_fetch_array($the_categories_date_query);
    return $the_categories_date['concert_date'] ?? ''; // Return empty string if no date is found
}

function tep_get_categories_time($categories_id) {
    global $FSESSION;
    $the_categories_time_query = tep_db_query("SELECT concert_time FROM " . TABLE_CATEGORIES_DESCRIPTION . " WHERE categories_id = '" . tep_db_input($categories_id) . "' AND language_id = '" . (int)$FSESSION->languages_id . "'");
    $the_categories_time = tep_db_fetch_array($the_categories_time_query);
    return $the_categories_time['concert_time'] ?? ''; // Return empty string if no time is found
}

function tep_get_plan_id($categories_id) {
    global $FSESSION;
    $the_plan_id_query = tep_db_query("SELECT plan_id FROM " . TABLE_CATEGORIES . " WHERE categories_id = '" . tep_db_input($categories_id) . "'");
    $the_plan_id = tep_db_fetch_array($the_plan_id_query);
    return $the_plan_id['plan_id'];
}



function categories_lookup() {
    global $id;

    $heading_name = $heading_venue = $heading_date = $heading_time = '';

    // Get the category ID for the product
    $products_category_id = tep_get_products_category_id($id);
    if (!$products_category_id) {
        return array('', '', '', '');
    }

    // Get the top-level category ID
    $top_category_id = tep_get_top_category_id($products_category_id);

    // Fetch category names
    $top_category_name = tep_get_categories_name($top_category_id);
    $sub_category_name = tep_get_categories_name($products_category_id);

    // Check if the product belongs to a sub-category
    $parent_id = tep_get_parent_id($products_category_id);
    $plan_id = tep_get_plan_id($products_category_id);

    // Determine date and time based on plan_id and parent_id
    if ($plan_id == 3) {
        // Fetch date and time from the sub-category (plan_id = 3)
        $heading_date = tep_get_categories_date($products_category_id);
        $heading_time = tep_get_categories_time($products_category_id);
    } elseif ($plan_id == 2) {
        // Fetch date and time from the parent category
        $heading_date = tep_get_categories_date($parent_id);
        $heading_time = tep_get_categories_time($parent_id);
    } else {
        // Default: Fetch date and time from the product's category
        $heading_date = tep_get_categories_date($products_category_id);
        $heading_time = tep_get_categories_time($products_category_id);
    }

    // Format date and time
    $heading_date = format_heading_date($heading_date);
    $heading_time = format_heading_time($heading_time);

    // Determine heading name and venue without overwriting inadvertently
    if ($plan_id == 3) {
        $heading_name = $top_category_name;// . ', ' . $sub_category_name;
        $heading_venue = tep_get_categories_venue($products_category_id);
        // Only use the top category venue if the sub-category venue is empty
        if (empty($heading_venue)) {
            $heading_venue = tep_get_categories_venue($top_category_id);
        }
    } elseif ($plan_id == 2) {
        $heading_name = $top_category_name;
        $heading_venue = tep_get_categories_venue($parent_id);
        if (empty($heading_venue)) {
            $heading_venue = tep_get_categories_venue($top_category_id);
        }
    } else {
        $heading_name = $top_category_name;
        $heading_venue = tep_get_categories_venue($top_category_id);
    }

    return array($heading_name, $heading_venue, $heading_date, $heading_time);
}
?>
