<?php
session_start();

// Check if the recipe session array exists, if not return an error
if (!isset($_SESSION['recipe'])) {
    echo json_encode(['status' => 'error', 'message' => 'No recipe found']);
    exit;
}

// Get the index of the ingredient to be removed
$index = $_POST['index'];

// Remove the ingredient from the session array
if (isset($_SESSION['recipe'][$index])) {
    unset($_SESSION['recipe'][$index]);
    // Re-index the array to prevent gaps
    $_SESSION['recipe'] = array_values($_SESSION['recipe']);
    echo json_encode(['status' => 'success', 'message' => 'Ingredient removed from recipe']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Ingredient not found']);
}
?>
