<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

statistics_module_boot();

$pageTitle = 'Player Statistics';
$competitionOptions = statistics_competition_options();
$selectedCompetitionId = (int) ($_GET['competition_id'] ?? 0);
$rows = player_statistics_rows($selectedCompetitionId > 0 ? $selectedCompetitionId : null);
$leaders = [
    'Goals' => statistics_leaders('goals', $selectedCompetitionId > 0 ? $selectedCompetitionId : null),
    'Assists' => statistics_leaders('assists', $selectedCompetitionId > 0 ? $selectedCompetitionId : null),
    'Yellow Cards' => statistics_leaders('yellow_cards', $selectedCompetitionId > 0 ? $selectedCompetitionId : null),
    'Appearances' => statistics_leaders('appearances', $selectedCompetitionId > 0 ? $selectedCompetitionId : null),
];

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Player statistics</h2>
        </div>
    </div>
    <form class="row g-3" method="get">
        <div class="col-md-8 col-lg-6">
            <label class="form-label" for="competition_id">Competition</label>
            <select class="form-select" id="competition_id" name="competition_id">
                <option value="">All competitions</option>
                <?php foreach ($competitionOptions as $competition): ?>
                    <option value="<?php echo e((string) $competition['id']); ?>" <?php echo $selectedCompetitionId === (int) $competition['id'] ? 'selected' : ''; ?>><?php echo e($competition['name']); ?> (<?php echo e($competition['season_name']); ?>)</option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4 col-lg-3 d-flex align-items-end gap-2">
            <button class="btn btn-jlfc" type="submit">Apply</button>
            <?php if ($selectedCompetitionId > 0): ?><a class="btn btn-link" href="<?php echo e(base_url('app/modules/statistics/index.php')); ?>">Reset</a><?php endif; ?>
        </div>
    </form>
</section>

<section class="row g-4 mb-4">
    <?php foreach ($leaders as $title => $items): ?>
        <div class="col-12 col-md-6 col-xl-3">
            <article class="panel-card h-100">
                <div class="panel-card-header mb-3">
                    <h3 class="h6 mb-0"><?php echo e($title); ?></h3>
                </div>
                <?php if ($items === []): ?>
                    <p class="text-muted mb-0">No data yet.</p>
                <?php else: ?>
                    <div class="d-flex flex-column gap-2">
                        <?php foreach ($items as $item): ?>
                            <div class="d-flex justify-content-between gap-3">
                                <div>
                                    <strong><?php echo e(trim($item['first_name'] . ' ' . $item['last_name'])); ?></strong>
                                    <div class="small text-muted"><?php echo e($item['club_name']); ?></div>
                                </div>
                                <span class="fw-semibold"><?php echo e((string) $item['metric_value']); ?></span>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </article>
        </div>
    <?php endforeach; ?>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Event-driven stats</p>
            <h3 class="h5 mb-0">Competition player totals</h3>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Player</th>
                    <th>Club</th>
                    <th>Competition</th>
                    <th>Apps</th>
                    <th>Goals</th>
                    <th>Assists</th>
                    <th>YC</th>
                    <th>RC</th>
                    <th>OG</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($rows === []): ?>
                    <tr>
                        <td colspan="9" class="text-center text-muted py-4">No player statistics are available yet.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($rows as $row): ?>
                    <tr>
                        <td><strong><?php echo e(trim($row['first_name'] . ' ' . $row['last_name'])); ?></strong>
                            <div class="small text-muted"><?php echo e($row['position']); ?></div>
                        </td>
                        <td><?php echo e($row['club_name']); ?></td>
                        <td><?php echo e($row['competition_name']); ?><div class="small text-muted"><?php echo e($row['season_name']); ?></div>
                        </td>
                        <td><?php echo e((string) $row['appearances']); ?></td>
                        <td><?php echo e((string) $row['goals']); ?></td>
                        <td><?php echo e((string) $row['assists']); ?></td>
                        <td><?php echo e((string) $row['yellow_cards']); ?></td>
                        <td><?php echo e((string) $row['red_cards']); ?></td>
                        <td><?php echo e((string) $row['own_goals']); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>