<?php
include 'db_connection.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
    exit();
}

$input = json_decode(file_get_contents('php://input'), true);
$test_id = isset($input['test_id']) ? (int)$input['test_id'] : null;

if (!$test_id) {
    echo json_encode(['success' => false, 'message' => 'Test ID is required.']);
    exit();
}

try {
    $stmt = $pdo->prepare("DELETE FROM tests WHERE id = :test_id");
    $stmt->bindParam(':test_id', $test_id, PDO::PARAM_INT);
    $stmt->execute();

    $stmt = $pdo->prepare("DELETE FROM grades WHERE test_id = :test_id");
    $stmt->bindParam(':test_id', $test_id, PDO::PARAM_INT);
    $stmt->execute();

    echo json_encode(['success' => true, 'message' => 'Test and associated grades deleted successfully.']);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
