<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/../licenses/helpers.php';

player_module_boot();

$playerRecord = find_player_or_abort((int) ($_GET['id'] ?? 0));
$documents = player_documents((int) $playerRecord['id']);
$playerLicenseReady = can_manage_player_licenses() && player_license_tables_ready();
$playerLicenseRecord = $playerLicenseReady ? get_player_license_by_player_id((int) $playerRecord['id']) : null;
$pageTitle = 'View Player';
$formErrors = flash('errors') ?? [];

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">Player 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-jlfc" href="<?php echo e(base_url('app/modules/players/edit.php?id=' . (int) $playerRecord['id'])); ?>">Edit</a>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/players/index.php?club_id=' . (int) $playerRecord['club_id'])); ?>">Club squad</a>
            <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/players/delete.php?id=' . (int) $playerRecord['id'])); ?>">Delete</a>
        </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>Player ID</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-credit-card-2-front"></i>
                <div>
                    <h4>NIN</h4>
                    <p><?php echo e($playerRecord['nin'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-diagram-3"></i>
                <div>
                    <h4>Position</h4>
                    <p><?php echo e($playerRecord['position']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-123"></i>
                <div>
                    <h4>Jersey number</h4>
                    <p><?php echo e((string) ($playerRecord['jersey_number'] ?? 'Not set')); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-calendar3"></i>
                <div>
                    <h4>Date of birth</h4>
                    <p><?php echo e($playerRecord['date_of_birth']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-globe2"></i>
                <div>
                    <h4>Nationality</h4>
                    <p><?php echo e($playerRecord['nationality'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-heart-pulse"></i>
                <div>
                    <h4>Medical</h4>
                    <p><span class="badge <?php echo e(player_badge_class($playerRecord['medical_status'])); ?>"><?php echo e(ucfirst($playerRecord['medical_status'])); ?></span></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <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 class="col-md-3">
            <div class="feature-tile"><i class="bi bi-arrow-left-right"></i>
                <div>
                    <h4>Transfer</h4>
                    <p><span class="badge <?php echo e(player_badge_class($playerRecord['transfer_status'])); ?>"><?php echo e(ucwords(str_replace('_', ' ', $playerRecord['transfer_status']))); ?></span></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(player_badge_class($playerRecord['status'])); ?>"><?php echo e(ucfirst($playerRecord['status'])); ?></span></p>
                </div>
            </div>
        </div>
    </div>
</section>

<?php if (can_manage_player_approval()): ?>
    <section class="panel-card mb-4">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Approval workflow</p>
                <h3 class="h5 mb-0">Eligibility controls</h3>
            </div>
        </div>
        <div class="d-flex flex-wrap gap-2">
            <?php foreach (player_eligibility_options() as $eligibilityStatus): ?>
                <form method="post" action="<?php echo e(base_url('app/modules/players/actions.php')); ?>">
                    <?php echo csrf_input(); ?>
                    <input type="hidden" name="action" value="set_eligibility_status">
                    <input type="hidden" name="id" value="<?php echo e((string) $playerRecord['id']); ?>">
                    <input type="hidden" name="eligibility_status" value="<?php echo e($eligibilityStatus); ?>">
                    <button class="btn btn-outline-jlfc" type="submit"><?php echo e(ucfirst($eligibilityStatus)); ?></button>
                </form>
            <?php endforeach; ?>
        </div>
    </section>
<?php endif; ?>

<?php if (can_manage_player_licenses()): ?>
    <section class="panel-card mb-4">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Licence workflow</p>
                <h3 class="h5 mb-0">Player licence</h3>
            </div>
            <div class="d-flex gap-2">
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/licenses/view.php?player_id=' . (int) $playerRecord['id'])); ?>">Manage licence</a>
                <?php if ($playerLicenseRecord !== 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>
        <?php if (!$playerLicenseReady): ?>
            <div class="alert alert-warning mb-0">Player licence services are temporarily unavailable.</div>
        <?php elseif ($playerLicenseRecord === null): ?>
            <p class="text-muted mb-0">No player licence has been issued yet.</p>
        <?php else: ?>
            <div class="row g-3">
                <div class="col-md-4">
                    <div class="feature-tile"><i class="bi bi-person-vcard-fill"></i>
                        <div>
                            <h4>Licence number</h4>
                            <p><?php echo e($playerLicenseRecord['license_number']); ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="feature-tile"><i class="bi bi-qr-code"></i>
                        <div>
                            <h4>Verification code</h4>
                            <p><?php echo e($playerLicenseRecord['verification_code']); ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="feature-tile"><i class="bi bi-patch-check-fill"></i>
                        <div>
                            <h4>Status</h4>
                            <p><span class="badge <?php echo e(player_license_badge_class(player_license_effective_status($playerLicenseRecord))); ?>"><?php echo e(player_license_status_label(player_license_effective_status($playerLicenseRecord))); ?></span></p>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    </section>
<?php endif; ?>

<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Document workflow</p>
            <h3 class="h5 mb-0">Player documents</h3>
        </div>
    </div>
    <form method="post" action="<?php echo e(base_url('app/modules/players/actions.php')); ?>" enctype="multipart/form-data" novalidate>
        <?php echo csrf_input(); ?>
        <input type="hidden" name="action" value="upload_document">
        <input type="hidden" name="player_id" value="<?php echo e((string) $playerRecord['id']); ?>">
        <div class="row g-3">
            <div class="col-md-4">
                <label class="form-label" for="document_type">Document type</label>
                <select class="form-select <?php echo validation_error($formErrors, 'document_type') ? 'is-invalid' : ''; ?>" id="document_type" name="document_type" required>
                    <?php foreach (player_document_type_options() as $documentType): ?>
                        <option value="<?php echo e($documentType); ?>"><?php echo e(ucwords(str_replace('_', ' ', $documentType))); ?></option>
                    <?php endforeach; ?>
                </select>
                <?php if (validation_error($formErrors, 'document_type')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'document_type')); ?></div><?php endif; ?>
            </div>
            <div class="col-md-4">
                <label class="form-label" for="title">Document title</label>
                <input class="form-control <?php echo validation_error($formErrors, 'title') ? 'is-invalid' : ''; ?>" id="title" name="title" value="<?php echo e((string) old('title')); ?>" required>
                <?php if (validation_error($formErrors, 'title')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'title')); ?></div><?php endif; ?>
            </div>
            <div class="col-md-4">
                <label class="form-label" for="document_file">File</label>
                <input class="form-control <?php echo validation_error($formErrors, 'document_file') ? 'is-invalid' : ''; ?>" id="document_file" name="document_file" type="file" accept=".pdf,.jpg,.jpeg,.png,.webp" required>
                <?php if (validation_error($formErrors, 'document_file')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'document_file')); ?></div><?php endif; ?>
            </div>
        </div>
        <div class="d-flex gap-2 mt-3">
            <button class="btn btn-jlfc" type="submit">Upload document</button>
        </div>
    </form>

    <hr>

    <?php if ($documents === []): ?>
        <p class="text-muted mb-0">No supporting documents uploaded yet.</p>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Type</th>
                        <th>Uploaded</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($documents as $document): ?>
                        <tr>
                            <td><?php echo e($document['title']); ?></td>
                            <td><?php echo e(ucwords(str_replace('_', ' ', $document['document_type']))); ?></td>
                            <td><?php echo e($document['uploaded_at']); ?></td>
                            <td class="text-end">
                                <div class="d-inline-flex gap-2">
                                    <a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url($document['file_path'])); ?>" target="_blank" rel="noopener">Open</a>
                                    <form method="post" action="<?php echo e(base_url('app/modules/players/actions.php')); ?>">
                                        <?php echo csrf_input(); ?>
                                        <input type="hidden" name="action" value="delete_document">
                                        <input type="hidden" name="document_id" value="<?php echo e((string) $document['id']); ?>">
                                        <button class="btn btn-outline-danger btn-sm" type="submit">Delete</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>