<?php
declare(strict_types=1);

require __DIR__ . '/../partials/auth.php';
require_once __DIR__ . '/../../config/database.php';

$pdo = getDbConnection();

$pageTitle = 'Quiz Challenge';
$pageSubtitle = 'Choose a Flashcard Set to create a vocabulary quiz';
$activeTeacherNav = 'quiz';

$q = trim((string)($_GET['q'] ?? ''));

$sql = "
    SELECT
        fs.id,
        fs.title,
        fs.description,
        COUNT(fsw.id) AS word_count
    FROM flashcard_sets fs
    LEFT JOIN flashcard_set_words fsw
        ON fsw.set_id = fs.id
";

$params = [];

if ($q !== '') {
    $sql .= " WHERE fs.title LIKE ? OR fs.description LIKE ?";
    $params = [
        '%' . $q . '%',
        '%' . $q . '%',
    ];
}

$sql .= "
    GROUP BY
        fs.id,
        fs.title,
        fs.description
    HAVING COUNT(fsw.id) >= 4
    ORDER BY
        fs.updated_at DESC,
        fs.id DESC
";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$sets = $stmt->fetchAll(PDO::FETCH_ASSOC);

ob_start();
?>
<link rel="stylesheet" href="../assets/css/quiz.css?v=1">

<div class="qz-manager-head">
    <div>
        <span class="section-kicker">Learning Game</span>
        <h2>Quiz Challenge</h2>
        <p>Choose an existing Flashcard Set. EnglishSpeak will create the questions automatically.</p>
    </div>

    <a class="button button-secondary" href="../flashcards/">
        Open Flashcards
    </a>
</div>

<section class="qz-summary-grid">
    <article>
        <span>Question types</span>
        <strong>4</strong>
        <small>Image, audio, meaning and English word</small>
    </article>

    <article>
        <span>Required words</span>
        <strong>4+</strong>
        <small>Enough words to create four answer choices</small>
    </article>

    <article>
        <span>Word source</span>
        <strong>Shared</strong>
        <small>Uses the existing Word Library</small>
    </article>
</section>

<form class="qz-manager-search" method="get">
    <input
        type="search"
        name="q"
        value="<?= htmlspecialchars($q, ENT_QUOTES, 'UTF-8') ?>"
        placeholder="Search Flashcard Sets..."
        autocomplete="off"
    >

    <?php if ($q !== ''): ?>
        <a class="button button-secondary" href="index.php">
            Clear
        </a>
    <?php endif; ?>
</form>

<div class="qz-set-grid">
    <?php if (!$sets): ?>
        <div class="qz-empty">
            <div class="qz-empty-icon">❓</div>

            <h3>
                <?= $q !== ''
                    ? 'No matching Flashcard Sets'
                    : 'No playable Flashcard Sets yet' ?>
            </h3>

            <p>
                A Quiz Challenge needs at least four words so each question can have four choices.
            </p>

            <a class="button button-primary" href="../flashcards/">
                Create or edit a Flashcard Set
            </a>
        </div>
    <?php endif; ?>

    <?php foreach ($sets as $set): ?>
        <article class="qz-set-card">
            <div class="qz-set-card-icon">❓</div>

            <div class="qz-set-card-copy">
                <h3>
                    <?= htmlspecialchars((string)$set['title'], ENT_QUOTES, 'UTF-8') ?>
                </h3>

                <p>
                    <?= htmlspecialchars(
                        (string)($set['description'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>
                </p>

                <div class="qz-set-meta">
                    <span><?= (int)$set['word_count'] ?> words</span>
                    <span>4 answer choices</span>
                </div>
            </div>

            <a
                class="button button-primary"
                href="play.php?set_id=<?= (int)$set['id'] ?>"
            >
                Start Quiz
            </a>
        </article>
    <?php endforeach; ?>
</div>

<?php
$content = ob_get_clean();
require __DIR__ . '/../layouts/app.php';
