<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_admin();

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    redirect('admin/quizzes.php');
}
verify_csrf();

$quizId = filter_input(INPUT_POST, 'quiz_id', FILTER_VALIDATE_INT);
$questionId = filter_input(INPUT_POST, 'question_id', FILTER_VALIDATE_INT);
$action = (string) ($_POST['action'] ?? '');
if (!$quizId || !$questionId) {
    flash('danger', 'بيانات السؤال غير صحيحة.');
    redirect('admin/quizzes.php');
}

if ($action === 'delete') {
    $stmt = db()->prepare('DELETE FROM quiz_questions WHERE id = ? AND quiz_id = ?');
    $stmt->execute([$questionId, $quizId]);
    flash('success', 'تم حذف السؤال.');
    redirect('admin/quiz_questions.php?quiz_id=' . $quizId);
}

flash('danger', 'الإجراء غير صحيح.');
redirect('admin/quiz_questions.php?quiz_id=' . $quizId);
