<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>

<div class="w-100 py-3 px-3">
    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3">
        <div>
            <h4 class="mb-1" style="font-family: Arial, sans-serif;">Competition Scores</h4>
            <div class="text-muted">Enter scores for your class competitions.</div>
        </div>
        <div class="text-end">
            <?php if (!empty($activeClassName)): ?>
                <div class="fw-semibold">Class: <?= esc($activeClassName) ?></div>
            <?php endif; ?>
            <?php if (!empty($schoolYear)): ?>
                <div class="text-muted small"><?= esc($schoolYear) ?> <?= esc($semester ?? '') ?></div>
            <?php endif; ?>
        </div>
    </div>

    <?php if (session('success')): ?>
        <div class="alert alert-success"><?= esc(session('success')) ?></div>
    <?php endif; ?>
    <?php if (session('error')): ?>
        <div class="alert alert-danger"><?= esc(session('error')) ?></div>
    <?php endif; ?>

    <?php if (empty($hasClasses)): ?>
        <div class="alert alert-warning">No classes assigned to you for the current term.</div>
    <?php elseif (empty($competitions)): ?>
        <div class="alert alert-info">No competitions available for this class right now.</div>
    <?php else: ?>
        <?php
        $formatDateLabel = function ($value) {
            if (!$value) return '';
            $value = (string)$value;
            if (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})/', $value, $m)) {
                return $m[2] . '-' . $m[3] . '-' . $m[1];
            }
            try {
                return local_date($value, 'm-d-Y');
            } catch (Throwable $e) {
                return $value;
            }
        };
        ?>
        <div class="table-responsive">
            <table class="table table-bordered table-sm align-middle">
                <thead class="table-light">
                    <tr>
                        <th>Title</th>
                        <th>Questions</th>
                        <th>Scores Entered</th>
                        <th>Dates</th>
                        <th>Locked</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($competitions as $competition): ?>
                    <?php
                        $compId = (int) ($competition['id'] ?? 0);
                        $sectionId = (int) ($competition['class_section_id'] ?? 0);
                        $sectionName = $sectionId > 0 ? ($sectionMap[$sectionId] ?? $sectionId) : 'All Classes';
                        $questionCount = $questionCounts[$compId] ?? null;
                        $scoreCount = $scoreCounts[$compId] ?? 0;
                        $scoreLabel = $studentTotal > 0 ? $scoreCount . ' / ' . $studentTotal : (string) $scoreCount;
                        $isLocked = !empty($competition['is_locked']);
                        $startDate = $competition['start_date'] ?? null;
                        $endDate = $competition['end_date'] ?? null;
                        $startLabel = $startDate ? $formatDateLabel($startDate) : '';
                        $endLabel = $endDate ? $formatDateLabel($endDate) : '';
                        if ($startLabel && $endLabel) {
                            $dateLabel = $startLabel . ' - ' . $endLabel;
                        } elseif ($startLabel) {
                            $dateLabel = $startLabel;
                        } elseif ($endLabel) {
                            $dateLabel = $endLabel;
                        } else {
                            $dateLabel = '—';
                        }
                    ?>
                    <tr>
                        <td><?= esc($competition['title'] ?? 'Competition') ?></td>                       
                        <td><?= $questionCount !== null ? esc($questionCount) : '—' ?></td>
                        <td><?= esc($scoreLabel) ?></td>
                        <td><?= esc($dateLabel) ?></td>
                        <td><?= $isLocked ? 'Yes' : 'No' ?></td>
                        <td>
                            <?php if ($isLocked): ?>
                                <button class="btn btn-sm btn-outline-secondary" type="button" disabled>Locked</button>
                            <?php else: ?>
                                <a class="btn btn-sm btn-outline-primary" href="<?= base_url('/teacher/competition-scores/' . $compId) ?>">
                                    Enter Scores
                                </a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>

<?= $this->endSection() ?>
