<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

fixture_module_boot();

$pageTitle = 'Fixtures';
$fixtures = fixture_list();
$competitionOptions = fixture_competition_options();
$selectedCompetitionId = (int) ($_GET['competition_id'] ?? 0);

if ($selectedCompetitionId > 0) {
    $fixtures = array_values(array_filter(
        $fixtures,
        static fn(array $fixture): bool => (int) $fixture['competition_id'] === $selectedCompetitionId
    ));
}

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>
            <p class="eyebrow mb-1">Match operations</p>
            <h2 class="h4 mb-0">Fixture management</h2>
        </div>
        <div class="d-flex gap-2 flex-wrap">
            <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/fixtures/create.php' . ($selectedCompetitionId > 0 ? '?competition_id=' . $selectedCompetitionId : ''))); ?>"><i class="bi bi-calendar-plus"></i> Add fixture</a>
            <?php if (can_generate_fixtures()): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/fixtures/generate.php' . ($selectedCompetitionId > 0 ? '?competition_id=' . $selectedCompetitionId : ''))); ?>"><i class="bi bi-magic"></i> Generate schedule</a>
            <?php endif; ?>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/fixtures/export-pdf.php' . ($selectedCompetitionId > 0 ? '?competition_id=' . $selectedCompetitionId : ''))); ?>"><i class="bi bi-file-earmark-pdf"></i> Download PDF</a>
        </div>
    </div>

    <form class="row g-3 mb-4" method="get">
        <div class="col-md-6 col-lg-4">
            <label class="form-label" for="competition_id">Filter by 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-6 col-lg-3 d-flex align-items-end gap-2">
            <button class="btn btn-outline-jlfc" type="submit">Apply filter</button>
            <?php if ($selectedCompetitionId > 0): ?>
                <a class="btn btn-link" href="<?php echo e(base_url('app/modules/fixtures/index.php')); ?>">Reset</a>
            <?php endif; ?>
        </div>
    </form>

    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Fixture</th>
                    <th>Competition</th>
                    <th>Schedule</th>
                    <th>Venue</th>
                    <th>Referee</th>
                    <th>Status</th>
                    <th>Result</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($fixtures === []): ?>
                    <tr>
                        <td colspan="8" class="text-center text-muted py-4">No fixtures found for the current filter.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($fixtures as $fixture): ?>
                    <tr>
                        <td>
                            <strong><?php echo e($fixture['home_club_name']); ?> vs <?php echo e($fixture['away_club_name']); ?></strong>
                            <div class="small text-muted">Matchday <?php echo e((string) $fixture['matchday']); ?><?php echo !empty($fixture['group_label']) ? ' • ' . e((string) $fixture['group_label']) : ''; ?><?php echo !empty($fixture['knockout_round']) ? ' • ' . e((string) $fixture['knockout_round']) : ''; ?></div>
                        </td>
                        <td>
                            <?php echo e($fixture['competition_name']); ?>
                            <div class="small text-muted"><?php echo e($fixture['season_name']); ?><?php echo ($fixture['season_status'] ?? '') === 'paused' ? ' • Season paused' : ''; ?></div>
                        </td>
                        <td>
                            <?php echo e($fixture['match_date'] ?: 'TBD'); ?>
                            <div class="small text-muted"><?php echo e(format_time_12h($fixture['match_time'] ?? null, 'Time pending')); ?></div>
                        </td>
                        <td><?php echo e($fixture['venue_name'] ?: 'Unassigned'); ?></td>
                        <td><?php echo e($fixture['referee_name'] ?: 'Unassigned'); ?></td>
                        <td>
                            <span class="badge <?php echo e(fixture_badge_class($fixture['status'])); ?>"><?php echo e(ucfirst($fixture['status'])); ?></span>
                            <?php if (!empty($fixture['postponement_reason'])): ?>
                                <div class="small text-muted mt-1"><?php echo e($fixture['postponement_reason']); ?></div>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($fixture['home_score'] !== null && $fixture['away_score'] !== null): ?>
                                <strong><?php echo e((string) $fixture['home_score']); ?> - <?php echo e((string) $fixture['away_score']); ?></strong>
                                <div class="small text-muted"><?php echo e(ucfirst((string) ($fixture['result_status'] ?? 'draft'))); ?></div>
                            <?php else: ?>
                                <span class="text-muted">Pending</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end">
                            <div class="btn-group btn-group-sm">
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/fixtures/view.php?id=' . (int) $fixture['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/fixtures/edit.php?id=' . (int) $fixture['id'])); ?>">Edit</a>
                                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/fixtures/delete.php?id=' . (int) $fixture['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>