<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

match_module_boot();

$pageTitle = 'Match Results';
$fixtures = match_result_list();

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">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Match results workflow</h2>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Fixture</th>
                    <th>Competition</th>
                    <th>Schedule</th>
                    <th>Result</th>
                    <th>Submission</th>
                    <th>Verification</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($fixtures === []): ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted py-4">No fixtures are available for result management yet.</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">Fixture #<?php echo e((string) $fixture['id']); ?></div>
                        </td>
                        <td>
                            <?php echo e($fixture['competition_name']); ?>
                            <div class="small text-muted"><?php echo e($fixture['season_name']); ?></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 if ($fixture['result_id'] !== null): ?>
                                <strong><?php echo e((string) $fixture['home_score']); ?> - <?php echo e((string) $fixture['away_score']); ?></strong>
                                <div class="small"><span class="badge <?php echo e(match_result_status_badge_class((string) $fixture['result_status'])); ?>"><?php echo e(ucfirst((string) $fixture['result_status'])); ?></span></div>
                            <?php else: ?>
                                <span class="text-muted">Pending</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($fixture['submitted_by_name']): ?>
                                <?php echo e($fixture['submitted_by_name']); ?>
                                <div class="small text-muted"><?php echo e($fixture['submitted_at'] ?: ''); ?></div>
                            <?php else: ?>
                                <span class="text-muted">Not submitted</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($fixture['verified_by_name']): ?>
                                <?php echo e($fixture['verified_by_name']); ?>
                                <div class="small text-muted"><?php echo e($fixture['verified_at'] ?: ''); ?></div>
                            <?php else: ?>
                                <span class="text-muted">Not verified</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end">
                            <a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/matches/view.php?fixture_id=' . (int) $fixture['id'])); ?>">Open</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>