<?php
ob_start();
session_start();
include "connection.php";
include "validation.php";
ob_clean();

header("Content-Type: application/json");

$response = [
    "status" => false,
    "message" => ""
];

try {
    if (!isset($_SESSION["user"]) || !isset($_SESSION["user"]["id"])) {
        $response["message"] = "Unauthorized access.";
        echo json_encode($response);
        exit;
    }

    $student_id = $_SESSION["user"]["id"];
    $student_id = intval($student_id);

    // Delete all pending requests (status = 1) for this student
    Database::iud("DELETE FROM `subject_requests` WHERE `student_id` = '" . $student_id . "' AND `request_status_id` = '1'");

    $response["status"] = true;
    $response["message"] = "All pending requests have been cancelled.";

} catch (Exception $e) {
    $response["message"] = "Error: " . $e->getMessage();
}

echo json_encode($response);
exit;

