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

// Check if 'id' is provided in the POST request
if (isset($_POST['id'])) {
    $ingredientId = $_POST['id'];

    // Check if the 'food_items' session variable exists
    if (isset($_SESSION['food_items'])) {
        // Get the current list of ingredients
        $ingredients = $_SESSION['food_items'];

        // Filter out the ingredient with the provided ID
        $ingredients = array_filter($ingredients, function($ingredient) use ($ingredientId) {
            return $ingredient['ingredient_id'] != $ingredientId;
        });

        // Reindex the array to maintain sequential numeric keys
        $ingredients = array_values($ingredients);

        // Update the session with the filtered ingredients
        $_SESSION['food_items'] = $ingredients;

        // Send success response
        echo json_encode(['success' => true]);
    } else {
        // Session variable not found
        echo json_encode(['success' => false, 'message' => 'No ingredients found in session.']);
    }
} else {
    // ID not provided
    echo json_encode(['success' => false, 'message' => 'Invalid request.']);
}
?>
