<?php

declare(strict_types=1);

require_once "../db.php";
require_once "../auth/customer_auth_common.php";
require_once "./soft_delete_helpers.php";

error_reporting(E_ERROR | E_PARSE);
ini_set("display_errors", "0");

if (!in_array(($_SERVER["REQUEST_METHOD"] ?? ""), ["DELETE", "POST"], true)) {
    customerAuthJsonResponse(405, [
        "success" => false,
        "message" => "Method not allowed",
    ]);
}

$session = customerAuthRequireSession($conn);
$input = customerAuthReadJsonInput();

$reservationIdRaw = $input["id"] ?? $input["reservationId"] ?? $_POST["id"] ?? $_POST["reservationId"] ?? ($_GET["id"] ?? $_GET["reservationId"] ?? 0);
$reservationId = is_numeric($reservationIdRaw) ? (int) $reservationIdRaw : 0;

if ($reservationId <= 0) {
    customerAuthJsonResponse(422, [
        "success" => false,
        "message" => "Invalid reservation ID.",
    ]);
}

$email = trim((string) ($session["email"] ?? ""));
if ($email === "") {
    customerAuthJsonResponse(401, [
        "success" => false,
        "message" => "Authentication required.",
    ]);
}

[$softDeleteReady, $softDeleteError] = reservationEnsureSoftDeleteColumns($conn);
if (!$softDeleteReady) {
    customerAuthJsonResponse(500, [
        "success" => false,
        "message" => $softDeleteError,
    ]);
}

$lookupStmt = $conn->prepare(
    "SELECT id, full_name, reservation_date, reservation_time, guests, status, user_deleted, admin_deleted
     FROM reservations
     WHERE id = ? AND email = ?
     LIMIT 1"
);
if (!$lookupStmt) {
    customerAuthJsonResponse(500, [
        "success" => false,
        "message" => "Failed to prepare reservation lookup.",
    ]);
}

$lookupStmt->bind_param("is", $reservationId, $email);
$lookupStmt->execute();
$lookupResult = $lookupStmt->get_result();
$reservation = $lookupResult ? $lookupResult->fetch_assoc() : null;
$lookupStmt->close();

if (!$reservation) {
    customerAuthJsonResponse(404, [
        "success" => false,
        "message" => "Reservation not found.",
    ]);
}

if ((int) ($reservation["admin_deleted"] ?? 0) === 1) {
    customerAuthJsonResponse(404, [
        "success" => false,
        "message" => "Reservation not found.",
    ]);
}

if ((int) ($reservation["user_deleted"] ?? 0) === 1) {
    customerAuthJsonResponse(200, [
        "success" => true,
        "message" => "Reservation already removed from your profile.",
    ]);
}

$deleteStmt = $conn->prepare(
    "UPDATE reservations
     SET user_deleted = 1
     WHERE id = ? AND email = ? AND user_deleted = 0
     LIMIT 1"
);
if (!$deleteStmt) {
    customerAuthJsonResponse(500, [
        "success" => false,
        "message" => "Failed to prepare reservation delete.",
    ]);
}
$deleteStmt->bind_param("is", $reservationId, $email);
$ok = $deleteStmt->execute();
$deleteStmt->close();

if (!$ok) {
    customerAuthJsonResponse(500, [
        "success" => false,
        "message" => "Unable to delete reservation.",
    ]);
}

customerAuthJsonResponse(200, [
    "success" => true,
    "message" => "Reservation removed from your profile.",
]);
