<?php
// CORS Headers - Essential for cross-origin requests
header("Access-Control-Allow-Origin: *"); // Or specify your domain
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, Accept, Origin");
header("Access-Control-Max-Age: 3600"); // Cache preflight for 1 hour

// Content Type
header("Content-Type: application/json; charset=UTF-8");

// Security Headers
header("X-Content-Type-Options: nosniff");
header("X-Frame-Options: DENY");
header("X-XSS-Protection: 1; mode=block");
header("Referrer-Policy: strict-origin-when-cross-origin");

// Handle OPTIONS preflight request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

// Cache Control (optional - adjust based on your needs)
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

require_once 'config.php';

// Fetch exclusive deals
$exclusive_deals = [];
$deals_result = $conn->query("SELECT * FROM app_menu_deals_items");
if ($deals_result && $deals_result->num_rows > 0) {
    while ($row = $deals_result->fetch_assoc()) {
        $exclusive_deals[] = $row;
    }
}

// Fetch featured dishes
$featured_dishes = [];
$featured_result = $conn->query("SELECT id, name, dish_description, price, category_id, image, is_featured, rating FROM menu_items WHERE is_featured = 1");
if ($featured_result && $featured_result->num_rows > 0) {
    while ($row = $featured_result->fetch_assoc()) {
        $featured_dishes[] = $row;
    }
}

$available_categories = [];
// Fetch available categories   
$categories_result = $conn->query("SELECT id, name FROM categories");
if ($categories_result && $categories_result->num_rows > 0) {
    while ($row = $categories_result->fetch_assoc()) {
        $available_categories[] = $row;
    }
}

// Output as JSON
echo json_encode([
    'success' => true,
    'exclusive_deals' => $exclusive_deals,
    'featured_dishes' => $featured_dishes,
    'available_categories' => $available_categories
]);
