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

<div class="container mt-4">
  <div class="d-flex justify-content-between align-items-center mb-2">
    <h3 class="mb-0">Preview Winners</h3>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary" href="/admin/competition-winners/<?= $competition['id'] ?>">Edit Scores</a>
      <a class="btn btn-outline-secondary" href="/admin/competition-winners/<?= $competition['id'] ?>/settings">Settings</a>
      <a class="btn btn-outline-secondary" href="/admin/competition-winners">Back</a>
    </div>
  </div>

  <div class="mb-3">
    <div><strong>Competition:</strong> <?= esc($competition['title']) ?></div>
    <div><strong>School Year:</strong> <?= esc($competition['school_year'] ?? '-') ?></div>
  </div>

  <?php if (empty($rankedByClass)): ?>
    <div class="alert alert-warning">No scores entered yet.</div>
  <?php else: ?>
    <?php foreach ($rankedByClass as $classId => $rows): ?>
      <?php
        $classLabel = $classMap[$classId] ?? ('Class ' . $classId);
        $topRows = $topByClass[$classId] ?? [];
        $winnerPrizeByStudent = [];
        foreach ($topRows as $winnerRow) {
          $studentId = $winnerRow['student_id'] ?? null;
          if ($studentId !== null) {
            $winnerPrizeByStudent[(int) $studentId] = $winnerRow['prize_amount'] ?? null;
          }
        }
        $studentCount = $classCounts[$classId] ?? count($rows);
        $override = $overrideMap[$classId] ?? null;
        $questions = $questionCounts[$classId] ?? null;
        $prizes = $prizeMap[$classId] ?? [];
        $finalWinners = $winnersCountByClass[$classId] ?? 0;
      ?>
      <h5 class="mt-4">Top Winners - <?= esc($classLabel) ?></h5>
      <div class="text-muted mb-2">
        Students: <?= esc($studentCount) ?> | Questions: <?= esc($questions ?? '-') ?> | Override: <?= esc($override ?? '-') ?> | Final Winners: <?= esc($finalWinners) ?>
      </div>
      <table class="table table-bordered table-sm" data-no-mgmt-sticky>
        <thead class="table-light">
          <tr><th>Rank</th><th>Student</th><th>Score</th><th>Prize</th></tr>
        </thead>
        <tbody>
        <?php foreach ($topRows as $row): ?>
          <tr>
            <td><?= esc($row['rank']) ?></td>
            <td><?= esc($row['name']) ?></td>
            <td><?= esc($row['score']) ?></td>
            <td><?= esc(!empty($row['prize_amount']) ? $row['prize_amount'] : '-') ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>

      <h6>All Ranked Scores - <?= esc($classLabel) ?></h6>
      <table class="table table-striped table-sm" data-no-mgmt-sticky>
        <thead><tr><th>#</th><th>Student</th><th>Score</th><th>Prize</th></tr></thead>
        <tbody>
        <?php $i = 1; foreach ($rows as $row): ?>
          <?php
            $studentId = (int) ($row['student_id'] ?? 0);
            $prizeVal = $winnerPrizeByStudent[$studentId] ?? null;
          ?>
          <tr>
            <td><?= $i++ ?></td>
            <td><?= esc($row['name']) ?></td>
            <td><?= esc($row['score']) ?></td>
            <td><?= esc(!empty($prizeVal) ? $prizeVal : '-') ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    <?php endforeach; ?>
  <?php endif; ?>

  <form method="post" action="/admin/competition-winners/<?= $competition['id'] ?>/publish" class="mt-3">
    <?= csrf_field() ?>
    <button class="btn btn-success">Publish Winners</button>
  </form>
</div>

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