<?php
// Include database connection or any necessary files
require_once('shikanisha.kts.php');

// Default values for pagination
$page = isset($_GET['page']) ? $_GET['page'] : 1;  // Get the current page
$limit = 10; // Number of items per page

// Define specific categories and their corresponding tables
$categories = array(
    'Beef' => 'food',
    'Chicken' => 'food',
    'Vegetables' => 'food',
    'Fish' => 'food',
    'Lunch' => 'food',
    'Beverages' => 'beverages',
    'Breakfast' => 'food',
    'Pizzas' => 'food',
    'Wraps' => 'food',
    'Pork' => 'food',
    'Cigarettes' => 'beverages' // Assuming cigarettes is a separate table
);

// Initialize an array to store fetched items
$allItems = array();

// Loop through each category and fetch all items with pagination
foreach ($categories as $category => $table) {

    // Calculate the total count of items for pagination
    if ($table === 'beverages') {
        $countQuery = "SELECT COUNT(*) as total FROM $table WHERE cat = '$category'";  // Adjusted for consistency
    } else {
        $countQuery = "SELECT COUNT(*) as total FROM food WHERE category = '$category'";  // Adjusted for consistency
    }

    $countResult = mysqli_query($conn, $countQuery);
    if (!$countResult) {
        die('Error executing count query: ' . mysqli_error($conn));
    }

    $rowCount = mysqli_fetch_assoc($countResult)['total'];

    // Calculate total pages based on total items and limit
    $totalPages = ceil($rowCount / $limit);

    // Calculate the offset for pagination (for skipping items based on page number)
    $offset = ($page - 1) * $limit;

    // Construct the query to fetch items based on the category and pagination
    if ($table === 'beverages') {
        $query = "SELECT `beverage_id` AS product_id, `beverage_name` AS product_name, `selling_price`, 
                  `image_location` AS image, `category` AS cat, `description` 
                  FROM `beverages` WHERE cat = '$category' LIMIT $limit OFFSET $offset";
    }else { // For 'food' table categories
        $query = "SELECT `food_id` AS product_id, `name` AS product_name, `price` AS selling_price, `image`, 
                  `description`, `category` AS cat FROM `food` WHERE category = '$category' LIMIT $limit OFFSET $offset";
    }

    // Execute the query to fetch items for the category
    $result = mysqli_query($conn, $query);
    if (!$result) {
        die('Error executing query: ' . mysqli_error($conn));
    }

    // Initialize an array to store the fetched items for this category
    $categoryItems = array();

    // Check if there are any items fetched for this category
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            // Add each fetched item to the category items array
            $categoryItems[] = $row;
        }
    }

    // Add category items and pagination info to the overall items array
    $allItems[$category] = array(
        'items' => $categoryItems,
        'total_items' => $rowCount,
        'total_pages' => $totalPages
    );
}

// Output the items and pagination info as a JSON response
echo json_encode($allItems);

// Close database connection
mysqli_close($conn);
?>
