<?php
header('Content-Type: application/json');
require_once 'config.php';

$category = $_GET['category'] ?? '';

if (!$category) {
    echo json_encode(['success' => false, 'message' => 'Category is required', 'dishes' => []]);
    exit;
}

// Step 1: Get category_id from categories table
$stmt = $conn->prepare("SELECT id FROM categories WHERE name = ? LIMIT 1");
$stmt->bind_param("s", $category);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();

if (!$row) {
    echo json_encode(['success' => false, 'message' => 'Category not found', 'dishes' => []]);
    exit;
}

$category_id = $row['id'];

// Step 2: Get dishes from menu_items where category_id = $category_id
$stmt = $conn->prepare("SELECT id, name, price, image, dish_description FROM menu_items WHERE category_id = ?");
$stmt->bind_param("i", $category_id);
$stmt->execute();
$result = $stmt->get_result();

$dishes = [];
while ($row = $result->fetch_assoc()) {
    $dishes[] = $row;
}
$stmt->close();

echo json_encode([
    'success' => true,
    'dishes' => $dishes
]);
