<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/core/helpers.php';
require_once __DIR__ . '/bootstrap.php';

$results = [];

try {
    $results = db_fetch_all(
        'SELECT f.id, f.match_date, f.stage_type, f.group_label, f.knockout_round, hc.name AS home_club, ac.name AS away_club,
            mr.home_score, mr.away_score, c.name AS competition_name, c.type AS competition_type
         FROM match_results mr
         INNER JOIN fixtures f ON f.id = mr.fixture_id
         INNER JOIN clubs hc ON hc.id = f.home_club_id
         INNER JOIN clubs ac ON ac.id = f.away_club_id
         INNER JOIN competitions c ON c.id = f.competition_id
         WHERE mr.result_status = :status
         ORDER BY f.match_date DESC, f.id DESC
         LIMIT 20',
        ['status' => 'verified']
    );
} catch (Throwable) {
}

render_public_page('Results', 'Verified results published from completed JLFC fixtures.', static function () use ($results): void {
?>
    <section class="panel-card">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Final scores</p>
                <h2 class="h4 mb-0">Verified match results</h2>
            </div>
        </div>
        <?php if ($results === []): ?>
            <p class="text-muted mb-0">No verified results are available yet.</p>
        <?php else: ?>
            <div class="row g-3">
                <?php foreach ($results as $result): ?>
                    <div class="col-md-6">
                        <div class="feature-tile">
                            <i class="bi bi-flag"></i>
                            <div class="w-100">
                                <p class="eyebrow mb-1"><?php echo e($result['competition_name']); ?></p>
                                <div class="small text-muted mb-1"><?php echo e(competition_stage_label($result)); ?></div>
                                <h3 class="h5 mb-1"><?php echo e($result['home_club']); ?> <?php echo e((string) $result['home_score']); ?> - <?php echo e((string) $result['away_score']); ?> <?php echo e($result['away_club']); ?></h3>
                                <p class="mb-2 text-muted"><?php echo e($result['match_date'] ?? ''); ?></p>
                                <a href="<?php echo e(base_url('app/public/match-details.php?id=' . (int) $result['id'])); ?>" class="btn btn-outline-jlfc btn-sm">Match details</a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>
<?php
});
