<?php
require_once('source/session.php');
require_once('source/error.php');
require_once('source/dbconfig.php');
require_once('source/function.php');
require_once('source/restrict_admin.php');

// Check if question ID is provided
if (!isset($_POST['id']) || !is_numeric($_POST['id'])) {
    sendJsonResponse(false, 'Invalid question ID.');
}

$question_id = $_POST['id'];

// Delete the question
try {
    $stmt = $DBH->prepare("DELETE FROM tbl_question WHERE id = :id");
    $stmt->execute(['id' => $question_id]);
    
    if ($stmt->rowCount() > 0) {
        sendJsonResponse(true, 'Question deleted successfully!');
    } else {
        sendJsonResponse(false, 'Question not found or already deleted.');
    }
} catch (PDOException $e) {
    sendJsonResponse(false, 'Database error: ' . $e->getMessage());
}

/**
 * Send JSON response
 */
function sendJsonResponse($success, $message) {
    header('Content-Type: application/json');
    echo json_encode([
        'success' => $success,
        'message' => $message
    ]);
    exit();
}
?>