<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

license_module_boot();

$playerRecord = find_player_or_abort((int) ($_GET['player_id'] ?? 0));
$tablesReady = player_license_tables_ready();
$license = $tablesReady ? get_player_license_by_player_id((int) $playerRecord['id']) : null;
$recentChecks = $tablesReady ? player_license_recent_checks((int) $playerRecord['id']) : [];
$pageTitle = 'Player Licence';

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">Licence profile</p>
            <h2 class="h4 mb-0"><?php echo e(player_full_name($playerRecord)); ?></h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/players/view.php?id=' . (int) $playerRecord['id'])); ?>">Back to player</a>
            <?php if ($license !== null): ?>
                <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/licenses/print.php?player_id=' . (int) $playerRecord['id'])); ?>" target="_blank" rel="noopener">Print licence</a>
            <?php endif; ?>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-buildings"></i>
                <div>
                    <h4>Club</h4>
                    <p><?php echo e($playerRecord['club_name']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-person-vcard"></i>
                <div>
                    <h4>Registration</h4>
                    <p><?php echo e($playerRecord['registration_number']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-patch-check"></i>
                <div>
                    <h4>Eligibility</h4>
                    <p><span class="badge <?php echo e(player_badge_class($playerRecord['eligibility_status'])); ?>"><?php echo e(ucfirst($playerRecord['eligibility_status'])); ?></span></p>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Admin workflow</p>
            <h3 class="h5 mb-0">Issue and control licence</h3>
        </div>
    </div>

    <?php if (!$tablesReady): ?>
        <div class="alert alert-warning mb-0">Player licence controls are temporarily unavailable.</div>
    <?php elseif ($license === null): ?>
        <p class="text-muted">No licence has been issued to this player yet.</p>
        <form method="post" action="<?php echo e(base_url('app/modules/licenses/actions.php')); ?>">
            <?php echo csrf_input(); ?>
            <input type="hidden" name="action" value="issue_license">
            <input type="hidden" name="player_id" value="<?php echo e((string) $playerRecord['id']); ?>">
            <button class="btn btn-jlfc" type="submit">Issue licence</button>
        </form>
    <?php else: ?>
        <?php $effectiveStatus = player_license_effective_status($license); ?>
        <div class="row g-3 mb-3">
            <div class="col-md-3">
                <div class="feature-tile"><i class="bi bi-person-vcard-fill"></i>
                    <div>
                        <h4>Licence number</h4>
                        <p><?php echo e($license['license_number']); ?></p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="feature-tile"><i class="bi bi-qr-code"></i>
                    <div>
                        <h4>Verification code</h4>
                        <p><?php echo e($license['verification_code']); ?></p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="feature-tile"><i class="bi bi-calendar-event"></i>
                    <div>
                        <h4>Issued</h4>
                        <p><?php echo e($license['issued_at']); ?></p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="feature-tile"><i class="bi bi-calendar-check"></i>
                    <div>
                        <h4>Expires</h4>
                        <p><?php echo e($license['expires_at']); ?></p>
                    </div>
                </div>
            </div>
        </div>

        <div class="d-flex flex-wrap gap-2 align-items-center">
            <span class="badge <?php echo e(player_license_badge_class($effectiveStatus)); ?>"><?php echo e(player_license_status_label($effectiveStatus)); ?></span>

            <form method="post" action="<?php echo e(base_url('app/modules/licenses/actions.php')); ?>">
                <?php echo csrf_input(); ?>
                <input type="hidden" name="action" value="issue_license">
                <input type="hidden" name="player_id" value="<?php echo e((string) $playerRecord['id']); ?>">
                <button class="btn btn-outline-jlfc" type="submit">Reissue licence</button>
            </form>

            <?php if ($effectiveStatus === 'active' || $effectiveStatus === 'expired'): ?>
                <form method="post" action="<?php echo e(base_url('app/modules/licenses/actions.php')); ?>">
                    <?php echo csrf_input(); ?>
                    <input type="hidden" name="action" value="set_license_status">
                    <input type="hidden" name="player_id" value="<?php echo e((string) $playerRecord['id']); ?>">
                    <input type="hidden" name="status" value="suspended">
                    <button class="btn btn-outline-warning" type="submit">Suspend</button>
                </form>
            <?php else: ?>
                <form method="post" action="<?php echo e(base_url('app/modules/licenses/actions.php')); ?>">
                    <?php echo csrf_input(); ?>
                    <input type="hidden" name="action" value="set_license_status">
                    <input type="hidden" name="player_id" value="<?php echo e((string) $playerRecord['id']); ?>">
                    <input type="hidden" name="status" value="active">
                    <button class="btn btn-outline-success" type="submit">Activate</button>
                </form>
            <?php endif; ?>

            <form method="post" action="<?php echo e(base_url('app/modules/licenses/actions.php')); ?>">
                <?php echo csrf_input(); ?>
                <input type="hidden" name="action" value="set_license_status">
                <input type="hidden" name="player_id" value="<?php echo e((string) $playerRecord['id']); ?>">
                <input type="hidden" name="status" value="revoked">
                <button class="btn btn-outline-danger" type="submit">Revoke</button>
            </form>
        </div>
    <?php endif; ?>
</section>

<?php if ($tablesReady): ?>
    <section class="panel-card mb-4">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Verification history</p>
                <h3 class="h5 mb-0">Recent match-day checks</h3>
            </div>
        </div>

        <?php if ($recentChecks === []): ?>
            <p class="text-muted mb-0">No fixture verification has been recorded for this player yet.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>Fixture</th>
                            <th>Date</th>
                            <th>Verified by</th>
                            <th>Verified at</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentChecks as $check): ?>
                            <tr>
                                <td><?php echo e($check['home_club_name'] . ' vs ' . $check['away_club_name']); ?></td>
                                <td><?php echo e(format_match_date_time($check['match_date'] ?? null, $check['match_time'] ?? null, 'Date TBC', 'Time TBC')); ?></td>
                                <td><?php echo e($check['verified_by_name'] ?: 'Unknown'); ?></td>
                                <td><?php echo e($check['verified_at']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </section>
<?php endif; ?>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>