<?php

declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';

$rows = [];

try {
    $rows = db_fetch_all(
        'SELECT lt.position, lt.played, lt.won, lt.drawn, lt.lost, lt.goals_for, lt.goals_against,
                lt.goal_difference, lt.points, lt.form, c.name AS club_name, cp.name AS competition_name
         FROM league_table lt
         INNER JOIN clubs c ON c.id = lt.club_id
         INNER JOIN competitions cp ON cp.id = lt.competition_id
         ORDER BY cp.name ASC, lt.position ASC
         LIMIT 50'
    );
} catch (Throwable) {
}

render_public_page('League Table', 'Standings generated from verified competition results.', static function () use ($rows): void {
?>
    <section class="panel-card">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Standings</p>
                <h2 class="h4 mb-0">Current table</h2>
            </div>
        </div>
        <?php if ($rows === []): ?>
            <p class="text-muted mb-0">The league table will appear after verified results are recorded.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>Pos</th>
                            <th>Club</th>
                            <th>P</th>
                            <th>W</th>
                            <th>D</th>
                            <th>L</th>
                            <th>GF</th>
                            <th>GA</th>
                            <th>GD</th>
                            <th>Pts</th>
                            <th>Form</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($rows as $row): ?>
                            <tr>
                                <td><?php echo e((string) $row['position']); ?></td>
                                <td>
                                    <strong><?php echo e($row['club_name']); ?></strong>
                                    <div class="small text-muted"><?php echo e($row['competition_name']); ?></div>
                                </td>
                                <td><?php echo e((string) $row['played']); ?></td>
                                <td><?php echo e((string) $row['won']); ?></td>
                                <td><?php echo e((string) $row['drawn']); ?></td>
                                <td><?php echo e((string) $row['lost']); ?></td>
                                <td><?php echo e((string) $row['goals_for']); ?></td>
                                <td><?php echo e((string) $row['goals_against']); ?></td>
                                <td><?php echo e((string) $row['goal_difference']); ?></td>
                                <td><strong><?php echo e((string) $row['points']); ?></strong></td>
                                <td><?php echo e($row['form'] ?? '-'); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </section>
<?php
});
