<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

$transferModuleBoot = 'transfer_module_boot';
$transferWindowReportList = 'transfer_window_report_list';
$transferWindowReportSummary = 'transfer_window_report_summary';
$transferWindowStatusBadgeClass = 'transfer_window_status_badge_class';

$transferModuleBoot();

if (!can_manage_transfer_windows()) {
    abort_request(403, 'Forbidden', 'Only league management can review transfer-window history.');
}

$pageTitle = 'Transfer Window Report';
$summary = $transferWindowReportSummary();
$windows = $transferWindowReportList();
$today = date('Y-m-d');

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="hero-banner mb-4">
    <div>
        <p class="eyebrow mb-2">League report</p>
        <h2>Transfer-window history</h2>
        <p class="mb-0 text-muted">Review every configured window, its season scope, deadline status, and how much business moved through it.</p>
    </div>
    <div class="hero-banner-meta">
        <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/transfers/index.php')); ?>">
            <i class="bi bi-arrow-left"></i>
            Back to transfers
        </a>
    </div>
</section>

<section class="row g-4 mb-4">
    <div class="col-12 col-md-6 col-xl-3">
        <article class="metric-card h-100">
            <p class="eyebrow mb-2">Windows</p>
            <h3 class="display-6 mb-1"><?php echo e((string) $summary['total_windows']); ?></h3>
            <p class="text-muted mb-0">Configured windows</p>
        </article>
    </div>
    <div class="col-12 col-md-6 col-xl-3">
        <article class="metric-card h-100">
            <p class="eyebrow mb-2">Live</p>
            <h3 class="display-6 mb-1"><?php echo e((string) $summary['open_windows']); ?></h3>
            <p class="text-muted mb-0">Open windows right now</p>
        </article>
    </div>
    <div class="col-12 col-md-6 col-xl-3">
        <article class="metric-card h-100">
            <p class="eyebrow mb-2">Closed</p>
            <h3 class="display-6 mb-1"><?php echo e((string) $summary['closed_windows']); ?></h3>
            <p class="text-muted mb-0">Archived windows</p>
        </article>
    </div>
    <div class="col-12 col-md-6 col-xl-3">
        <article class="metric-card h-100">
            <p class="eyebrow mb-2">Drafts</p>
            <h3 class="display-6 mb-1"><?php echo e((string) $summary['draft_windows']); ?></h3>
            <p class="text-muted mb-0">Waiting to be opened</p>
        </article>
    </div>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Window log</p>
            <h3 class="h5 mb-0">Configured transfer windows</h3>
        </div>
    </div>

    <?php if ($windows === []): ?>
        <p class="text-muted mb-0">No transfer windows have been configured yet.</p>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Window</th>
                        <th>Season</th>
                        <th>Dates</th>
                        <th>Status</th>
                        <th>Deadline note</th>
                        <th>Transfers</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($windows as $window): ?>
                        <?php
                        $deadlineNote = 'Upcoming';
                        if ((string) $window['status'] === 'closed') {
                            $deadlineNote = (string) $window['end_date'] < $today ? 'Closed after deadline' : 'Closed manually or early';
                        } elseif ((string) $window['status'] === 'open') {
                            $deadlineNote = (string) $window['end_date'] < $today ? 'Awaiting auto-close sync' : 'Running live';
                        }
                        ?>
                        <tr>
                            <td>
                                <strong><?php echo e($window['name']); ?></strong>
                                <div class="small text-muted">Created <?php echo e((string) $window['created_at']); ?></div>
                            </td>
                            <td><?php echo e($window['season_name'] ?? 'All seasons'); ?></td>
                            <td><?php echo e((string) $window['start_date']); ?> to <?php echo e((string) $window['end_date']); ?></td>
                            <td><span class="badge <?php echo e($transferWindowStatusBadgeClass((string) $window['status'])); ?>"><?php echo e(ucfirst((string) $window['status'])); ?></span></td>
                            <td><?php echo e($deadlineNote); ?></td>
                            <td>
                                <strong><?php echo e((string) $window['total_transfers']); ?></strong>
                                <div class="small text-muted">
                                    <?php echo e((string) $window['active_transfers']); ?> active ·
                                    <?php echo e((string) $window['completed_transfers']); ?> completed ·
                                    <?php echo e((string) $window['failed_transfers']); ?> failed
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>