<?php
header('Content-Type: application/json');
session_start();
include 'shikanisha.kts.php';

$food_id = $_GET['food_id'];

// Fetch food details
$sql_food = "SELECT food_id, name, price, food_group, category FROM food WHERE food_id = ?";
$stmt_food = $conn->prepare($sql_food);
$stmt_food->bind_param("i", $food_id);
$stmt_food->execute();
$result_food = $stmt_food->get_result();
$food = $result_food->fetch_assoc();

// Fetch ingredients details
$sql_ingredients = "SELECT fi.id, fi.ingredient_id, fi.quantity_required, i.buying_price, (fi.quantity_required * i.buying_price) AS cost_price 
                    FROM food_ingredients fi 
                    JOIN ingredients i ON fi.ingredient_id = i.id 
                    WHERE fi.food_id = ?";
$stmt_ingredients = $conn->prepare($sql_ingredients);
$stmt_ingredients->bind_param("i", $food_id);
$stmt_ingredients->execute();
$result_ingredients = $stmt_ingredients->get_result();
$ingredients = [];
while ($row = $result_ingredients->fetch_assoc()) {
    $ingredients[] = $row;
}

// Store only the ingredients in the session
$_SESSION['food_items'] = $ingredients;

// Prepare the response
$response = ['status' => 'success', 'data' => ['food' => $food, 'ingredients' => $ingredients]];


echo json_encode($response);
?>