<?php
session_start();
include '../admin/includes/database.php';
include '../admin/includes/functions.php';

$response = ['status' => 'error', 'message' => 'Invalid request'];

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
    try {
        $db = new Database();
        $question_id = $_POST['id'];

        // Check if the question exists
        $db->query("SELECT * FROM cbt_questions WHERE id = :question_id");
        $db->bind(':question_id', $question_id);
        $question = $db->fetchSingle();

        if (!$question) {
            throw new Exception("Question not found.");
        }

        // Delete question options if MCQ
        if ($question->question_type === 'mcq') {
            $db->query("DELETE FROM cbt_options WHERE question_id = :question_id");
            $db->bind(':question_id', $question_id);
            $db->execute();
        }

        // Delete the question
        $db->query("DELETE FROM cbt_questions WHERE id = :question_id");
        $db->bind(':question_id', $question_id);
        $db->execute();

        $response = ['status' => 'success', 'message' => 'Question deleted successfully.'];
    } catch (Exception $e) {
        $response['message'] = $e->getMessage();
    }
}

echo json_encode($response);



