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

$quizId = filter_input(INPUT_GET, 'quiz_id', FILTER_VALIDATE_INT);
if (!$quizId) {
    flash('danger', 'رقم الاختبار غير صحيح.');
    redirect('admin/quizzes.php');
}

$stmt = db()->prepare('SELECT * FROM quizzes WHERE id = ? LIMIT 1');
$stmt->execute([$quizId]);
$quiz = $stmt->fetch();
if (!$quiz) {
    flash('danger', 'الاختبار غير موجود.');
    redirect('admin/quizzes.php');
}

$stmt = db()->prepare(
    'SELECT qq.*,
            (SELECT qo.option_text FROM quiz_options qo WHERE qo.question_id = qq.id AND qo.is_correct = 1 ORDER BY qo.sort_order LIMIT 1) AS correct_answer
     FROM quiz_questions qq
     WHERE qq.quiz_id = ?
     ORDER BY qq.sort_order, qq.id'
);
$stmt->execute([$quizId]);
$questions = $stmt->fetchAll();

$pageTitle = 'أسئلة الاختبار';
require_once __DIR__ . '/../includes/header.php';
?>
<div class="dashboard-head">
    <div>
        <span class="dashboard-kicker"><?= e(grade_text($quiz['grade'])) ?></span>
        <h1><?= e($quiz['title']) ?></h1>
        <p><?= count($questions) ?> سؤال — <?= (int) $quiz['is_free'] === 1 ? 'مجاني' : 'للمشتركين فقط' ?></p>
    </div>
    <div class="actions compact-actions">
        <a class="btn btn-primary" href="<?= e(url('admin/quiz_question_form.php?quiz_id=' . $quizId)) ?>">إضافة سؤال</a>
        <a class="btn btn-outline" href="<?= e(url('admin/quiz_form.php?id=' . $quizId)) ?>">تعديل الاختبار</a>
        <a class="btn btn-outline" href="<?= e(url('admin/quizzes.php')) ?>">كل الاختبارات</a>
    </div>
</div>

<section class="card">
    <?php if (!$questions): ?>
        <div class="empty-state question-empty">
            <img src="<?= e(url('assets/images/kodo-logo.png')) ?>" alt="كودو">
            <div><h2>ابدأ بإضافة أول سؤال</h2><p>كل سؤال يحتوي على أربع إجابات، وتحدد إجابة صحيحة واحدة.</p></div>
        </div>
    <?php else: ?>
        <div class="admin-question-list">
            <?php foreach ($questions as $index => $question): ?>
                <article class="admin-question-card">
                    <span class="question-index"><?= $index + 1 ?></span>
                    <div class="admin-question-copy">
                        <strong><?= e($question['question_text']) ?></strong>
                        <span>الإجابة الصحيحة: <b><?= e((string) $question['correct_answer']) ?></b></span>
                        <?php if (!empty($question['hint_text'])): ?>
                            <small class="admin-hint-preview">تلميح كودو: <?= e($question['hint_text']) ?></small>
                        <?php endif; ?>
                        <?php if (!empty($question['explanation'])): ?>
                            <small>التوضيح: <?= e($question['explanation']) ?></small>
                        <?php endif; ?>
                    </div>
                    <div class="actions compact-actions">
                        <a class="btn btn-small btn-outline" href="<?= e(url('admin/quiz_question_form.php?quiz_id=' . $quizId . '&id=' . (int) $question['id'])) ?>">تعديل</a>
                        <form method="post" action="<?= e(url('admin/quiz_question_action.php')) ?>" onsubmit="return confirm('هل تريد حذف هذا السؤال؟')">
                            <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
                            <input type="hidden" name="quiz_id" value="<?= $quizId ?>">
                            <input type="hidden" name="question_id" value="<?= (int) $question['id'] ?>">
                            <input type="hidden" name="action" value="delete">
                            <button class="btn btn-small btn-danger" type="submit">حذف</button>
                        </form>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</section>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
