<?php
declare(strict_types=1);

/**
 * EnglishSpeak — Speaking Challenge
 * Select a Flashcard Set and start speaking practice.
 */

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

$pdo = getDbConnection();

$pageTitle = 'Speaking Challenge';
$pageSubtitle = 'Listen, record and practise pronunciation';
$activeTeacherNav = 'speaking';

$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) >= 1
    ORDER BY
        fs.updated_at DESC,
        fs.id DESC
";

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

ob_start();
?>
<style>
.sp-index-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 18px;
    margin-bottom: 18px;
}

.sp-index-head h2 {
    margin: 4px 0;
}

.sp-index-head p {
    max-width: 700px;
    margin: 5px 0;
    color: var(--muted);
}

.sp-index-search {
    display: flex;
    gap: 10px;
    margin-bottom: 18px;
}

.sp-index-search input {
    width: min(470px, 100%);
    padding: 11px 13px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: #ffffff;
}

.sp-summary-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    margin-bottom: 18px;
}

.sp-summary-card {
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 15px;
    background: var(--surface);
}

.sp-summary-card span,
.sp-summary-card small {
    display: block;
    color: var(--muted);
}

.sp-summary-card span {
    font-size: 12px;
    font-weight: 700;
}

.sp-summary-card strong {
    display: block;
    margin: 7px 0;
    color: #2563eb;
    font-size: 25px;
}

.sp-set-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
    gap: 16px;
}

.sp-set-card {
    display: grid;
    grid-template-columns: 58px minmax(0, 1fr);
    gap: 14px;
    align-items: start;
    padding: 18px;
    border: 1px solid var(--border);
    border-radius: 17px;
    background: var(--surface);
    box-shadow: 0 6px 20px rgba(49, 76, 123, .06);
}

.sp-set-icon {
    display: grid;
    width: 54px;
    height: 54px;
    place-items: center;
    border-radius: 15px;
    background: #f1ecff;
    font-size: 27px;
}

.sp-set-copy {
    min-width: 0;
}

.sp-set-card h3 {
    margin: 0 0 5px;
}

.sp-set-card p {
    min-height: 38px;
    margin: 0 0 10px;
    overflow: hidden;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.45;
}

.sp-set-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
}

.sp-set-meta span {
    padding: 5px 8px;
    border-radius: 999px;
    color: #2563eb;
    background: #f3f6ff;
    font-size: 11px;
    font-weight: 700;
}

.sp-set-card > .button {
    grid-column: 1 / -1;
    width: 100%;
}

.sp-empty {
    grid-column: 1 / -1;
    padding: 50px 24px;
    border: 1px solid var(--border);
    border-radius: 18px;
    background: var(--surface);
    text-align: center;
}

.sp-empty-icon {
    font-size: 50px;
}

.sp-empty h3 {
    margin: 10px 0 7px;
}

.sp-empty p {
    margin: 0 auto 18px;
    max-width: 520px;
    color: var(--muted);
    line-height: 1.6;
}

@media (max-width: 760px) {
    .sp-index-head {
        flex-direction: column;
    }

    .sp-summary-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 430px) {
    .sp-set-grid {
        grid-template-columns: 1fr;
    }
}
</style>

<div class="sp-index-head">
    <div>
        <span class="section-kicker">Speaking Practice</span>
        <h2>Speaking Challenge</h2>
        <p>
            Choose a Flashcard Set. Students can listen, record their voice,
            replay it and practise each word.
        </p>
    </div>

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

<section class="sp-summary-grid">
    <article class="sp-summary-card">
        <span>Practice flow</span>
        <strong>3 steps</strong>
        <small>Listen, speak and replay</small>
    </article>

    <article class="sp-summary-card">
        <span>Word source</span>
        <strong>Shared</strong>
        <small>Uses the existing Word Library</small>
    </article>

    <article class="sp-summary-card">
        <span>Device</span>
        <strong>Mobile</strong>
        <small>Optimized for phone microphones</small>
    </article>
</section>

<form class="sp-index-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="sp-set-grid">
    <?php if (!$sets): ?>
        <div class="sp-empty">
            <div class="sp-empty-icon">🎤</div>

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

            <p>
                Add at least one active word to a Flashcard Set,
                then return here to start Speaking Challenge.
            </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="sp-set-card">
            <div class="sp-set-icon">🎤</div>

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

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

                <div class="sp-set-meta">
                    <span><?= (int)$set['word_count'] ?> words</span>
                    <span>Listen and record</span>
                </div>
            </div>

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

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