<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

referee_module_boot();

$refereeRecord = find_referee_or_abort((int) ($_GET['id'] ?? 0));
$assignments = db_fetch_all(
    'SELECT f.id, f.matchday, f.match_date, hc.name AS home_club, ac.name AS away_club, c.name AS competition_name
     FROM fixtures f
     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 f.referee_id = :referee_id
     ORDER BY f.match_date DESC, f.id DESC
     LIMIT 10',
    ['referee_id' => $refereeRecord['id']]
);
$pageTitle = 'View Referee';

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">Referee profile</p>
            <h2 class="h4 mb-0"><?php echo e($refereeRecord['full_name']); ?></h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/referees/edit.php?id=' . (int) $refereeRecord['id'])); ?>">Edit</a>
            <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/referees/delete.php?id=' . (int) $refereeRecord['id'])); ?>">Delete</a>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-award"></i>
                <div>
                    <h4>Level</h4>
                    <p><?php echo e(ucfirst($refereeRecord['level'])); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-telephone"></i>
                <div>
                    <h4>Phone</h4>
                    <p><?php echo e($refereeRecord['phone'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-envelope"></i>
                <div>
                    <h4>Email</h4>
                    <p><?php echo e($refereeRecord['email'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-flag"></i>
                <div>
                    <h4>Status</h4>
                    <p><span class="badge <?php echo e(referee_badge_class($refereeRecord['status'])); ?>"><?php echo e(ucfirst($refereeRecord['status'])); ?></span></p>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Assignments</p>
            <h3 class="h5 mb-0">Recent appointed fixtures</h3>
        </div>
        <?php if (can_assign_match_referees()): ?>
            <span class="badge text-bg-light">Assignment-ready</span>
        <?php endif; ?>
    </div>
    <?php if ($assignments === []): ?>
        <p class="text-muted mb-0">No fixtures are assigned to this referee yet.</p>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Competition</th>
                        <th>Fixture</th>
                        <th>Date</th>
                        <th>Matchday</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($assignments as $fixture): ?>
                        <tr>
                            <td><?php echo e($fixture['competition_name']); ?></td>
                            <td><?php echo e($fixture['home_club']); ?> vs <?php echo e($fixture['away_club']); ?></td>
                            <td><?php echo e($fixture['match_date'] ?: 'TBD'); ?></td>
                            <td><?php echo e((string) $fixture['matchday']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>