<?= $this->extend('layout/app') ?>

<?= $this->section('title') ?>Training Quizzes<?= $this->endSection() ?>
<?= $this->section('pageTitle') ?>Training Quizzes<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="space-y-6">
    <div class="flex flex-wrap items-center justify-between gap-3">
        <p class="text-sm text-slate-500">You can take any quiz multiple times — each try pulls fresh AI-generated questions from the trained documents.</p>
    </div>

    <?php if (empty($quizzes)): ?>
        <div class="glass rounded-2xl border border-slate-200/60 p-16 text-center">
            <div class="mx-auto w-16 h-16 rounded-2xl bg-slate-100 flex items-center justify-center mb-4">
                <svg xmlns="http://www.w3.org/2000/svg" class="w-8 h-8 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M9.75 3.104v5.714a2.25 2.25 0 01-.659 1.591L5 14.5m9.75-11.396v5.714a2.25 2.25 0 00.659 1.591L19 14.5" /></svg>
            </div>
            <p class="text-sm font-medium text-slate-600">No quizzes are available yet. Ask your admin to create one in Quiz Settings.</p>
        </div>
    <?php else: ?>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
            <?php foreach ($quizzes as $q):
                $type = ($q['question_type'] ?? '') === 'fill_up' ? 'Fill in the Blank' : 'Multiple Choice';
                $typeClass = ($q['question_type'] ?? '') === 'fill_up' ? 'bg-sky-100 text-sky-700' : 'bg-indigo-100 text-indigo-700';
                $best = $q['my_best_score'];
                $last = $q['my_last_score'];
                $n = (int)($q['attempt_count'] ?? 0);
            ?>
                <div class="glass rounded-2xl border border-slate-200/60 shadow-sm overflow-hidden hover:shadow-md transition-all">
                    <div class="p-5">
                        <div class="flex flex-wrap items-start justify-between gap-3 mb-3">
                            <div class="flex flex-col">
                                <span class="inline-flex items-center gap-2 mb-1">
                                    <h3 class="text-base font-bold text-slate-800"><?= esc($q['title']) ?></h3>
                                    <span class="inline-flex px-2 py-0.5 rounded-lg text-[10px] font-semibold <?= $typeClass ?>"><?= $type ?></span>
                                </span>
                                <?php if (!empty($q['description'])): ?>
                                    <p class="text-xs text-slate-500 line-clamp-2"><?= esc($q['description']) ?></p>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="grid grid-cols-4 gap-3 mb-4">
                            <div class="bg-slate-50 rounded-xl p-2.5 text-center">
                                <p class="text-[10px] font-semibold text-slate-500 uppercase tracking-wide">Questions</p>
                                <p class="text-sm font-bold text-slate-800 mt-0.5"><?= (int)($q['question_count'] ?? 0) ?></p>
                            </div>
                            <div class="bg-slate-50 rounded-xl p-2.5 text-center">
                                <p class="text-[10px] font-semibold text-slate-500 uppercase tracking-wide">Timer</p>
                                <p class="text-sm font-bold text-slate-800 mt-0.5"><?= (int)($q['question_timer_seconds'] ?? 0) ?>s</p>
                            </div>
                            <div class="bg-slate-50 rounded-xl p-2.5 text-center">
                                <p class="text-[10px] font-semibold text-slate-500 uppercase tracking-wide">Passing</p>
                                <p class="text-sm font-bold text-slate-800 mt-0.5"><?= (int)($q['passing_score_pct'] ?? 0) ?>%</p>
                            </div>
                            <div class="bg-slate-50 rounded-xl p-2.5 text-center">
                                <p class="text-[10px] font-semibold text-slate-500 uppercase tracking-wide">Attempts</p>
                                <p class="text-sm font-bold text-slate-800 mt-0.5"><?= $n ?></p>
                            </div>
                        </div>
                        <?php if (!empty($q['docNames'])): ?>
                            <p class="text-[11px] text-slate-500 mb-4" title="<?= esc(implode(', ', $q['docNames'])) ?>">
                                <span class="font-semibold text-slate-600">Docs:</span> <?= esc(implode(', ', array_slice($q['docNames'], 0, 3))) ?><?= count($q['docNames']) > 3 ? ' (+' . (count($q['docNames']) - 3) . ' more)' : '' ?>
                            </p>
                        <?php endif; ?>

                        <?php if ($n > 0): ?>
                            <div class="flex flex-wrap gap-2 mb-4">
                                <div class="px-3 py-1.5 rounded-xl bg-violet-50 text-violet-700 text-[11px] font-semibold">
                                    Best: <span class="font-bold"><?= $best !== null ? number_format((float)$best, 1) . '%' : '—' ?></span>
                                </div>
                                <div class="px-3 py-1.5 rounded-xl bg-slate-100 text-slate-700 text-[11px] font-semibold">
                                    Last: <span class="font-bold"><?= $last !== null ? number_format((float)$last, 1) . '%' : '—' ?></span>
                                </div>
                                <?php if ($best !== null && (float)$best >= (float)($q['passing_score_pct'] ?? 0)): ?>
                                    <div class="px-3 py-1.5 rounded-xl bg-emerald-50 text-emerald-700 text-[11px] font-semibold">✓ Passed</div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <div class="flex flex-wrap items-center gap-2">
                            <a href="<?= site_url('/agent/quizzes/start/' . (int)$q['id']) ?>"
                               data-quiz-start-btn
                               class="inline-flex items-center gap-2 px-4 py-2.5 bg-gradient-to-r from-violet-600 to-fuchsia-600 text-white text-sm font-semibold rounded-xl shadow-md shadow-violet-500/20 hover:shadow-lg hover:-translate-y-0.5 active:translate-y-0 transition-all">
                                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M5.25 5.653c0-.856.917-1.398 1.667-.986l11.54 6.348a1.125 1.125 0 010 1.971l-11.54 6.347a1.125 1.125 0 01-1.667-.986V5.653z" /></svg>
                                <span class="quiz-start-label"><?= $n > 0 ? 'Retry Quiz' : 'Start Quiz' ?></span>
                            </a>
                            <?php if ($n > 0): ?>
                                <p class="text-[11px] text-slate-500">Each try generates brand new questions from the docs.</p>
                            <?php else: ?>
                                <p class="text-[11px] text-slate-500">AI will pull unique questions on every attempt.</p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
<script>
(function () {
  const SPINNER = '<svg class="w-4 h-4 animate-spin" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-85" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"></path></svg>';
  document.querySelectorAll('a[data-quiz-start-btn]').forEach(function (btn) {
    btn.addEventListener('click', function (e) {
      if (btn.dataset.loading === '1') { e.preventDefault(); return; }
      btn.dataset.loading = '1';
      e.preventDefault();
      const href = btn.getAttribute('href') || '#';
      const label = btn.querySelector('.quiz-start-label');
      btn.classList.add('opacity-80', 'cursor-wait', 'pointer-events-none');
      btn.style.transform = 'none';
      btn.style.boxShadow = 'none';
      btn.innerHTML = SPINNER + ' <span class="quiz-start-label inline-flex">' + (label ? 'Generating questions…' : 'Generating…') + '</span>';
      setTimeout(function () { window.location.href = href; }, 30);
    });
  });
})();
</script>
<?= $this->endSection() ?>
