<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

license_module_boot();

$pageTitle = 'Player Licences';
$tablesReady = player_license_tables_ready();
$licenses = $tablesReady ? player_license_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 mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">League documents</p>
            <h2 class="h4 mb-0">Player licence management</h2>
        </div>
        <?php if (can_verify_player_licenses()): ?>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/licenses/verify.php')); ?>">Open verification dashboard</a>
        <?php endif; ?>
    </div>

    <?php if (!$tablesReady): ?>
        <div class="alert alert-warning mb-0">Player licence services are temporarily unavailable.</div>
    <?php elseif ($licenses === []): ?>
        <p class="text-muted mb-0">No players are available for licensing yet.</p>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Player</th>
                        <th>Club</th>
                        <th>Registration</th>
                        <th>Licence</th>
                        <th>Status</th>
                        <th>Expiry</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($licenses as $licenseRow): ?>
                        <?php $effectiveStatus = player_license_effective_status($licenseRow['license_id'] === null ? null : ['id' => $licenseRow['license_id'], 'status' => $licenseRow['license_status'], 'expires_at' => $licenseRow['expires_at']]); ?>
                        <tr>
                            <td><?php echo e(trim($licenseRow['first_name'] . ' ' . $licenseRow['last_name'])); ?></td>
                            <td><?php echo e($licenseRow['club_name']); ?></td>
                            <td><?php echo e($licenseRow['registration_number']); ?></td>
                            <td><?php echo e($licenseRow['license_number'] ?? 'Not issued'); ?></td>
                            <td><span class="badge <?php echo e(player_license_badge_class($effectiveStatus)); ?>"><?php echo e(player_license_status_label($effectiveStatus)); ?></span></td>
                            <td><?php echo e($licenseRow['expires_at'] ?: 'Not set'); ?></td>
                            <td class="text-end">
                                <div class="d-inline-flex gap-2">
                                    <a class="btn btn-outline-jlfc btn-sm" href="<?php echo e(base_url('app/modules/licenses/view.php?player_id=' . (int) $licenseRow['player_id'])); ?>">Manage</a>
                                    <a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/players/view.php?id=' . (int) $licenseRow['player_id'])); ?>">Player</a>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>