<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

season_module_boot();

$seasonRecord = find_season_or_abort((int) ($_GET['id'] ?? 0));
$competitions = db_fetch_all(
    'SELECT id, name, type, status, number_of_teams
     FROM competitions
     WHERE season_id = :season_id
     ORDER BY created_at DESC, id DESC',
    ['season_id' => $seasonRecord['id']]
);
$pageTitle = 'View Season';

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">Season profile</p>
            <h2 class="h4 mb-0"><?php echo e($seasonRecord['name']); ?></h2>
        </div>
        <div class="d-flex gap-2 flex-wrap">
            <?php if ($seasonRecord['status'] === 'active'): ?>
                <form method="post" action="<?php echo e(base_url('app/modules/seasons/actions.php')); ?>" class="d-inline">
                    <?php echo csrf_input(); ?>
                    <input type="hidden" name="action" value="set_status">
                    <input type="hidden" name="id" value="<?php echo e((string) $seasonRecord['id']); ?>">
                    <input type="hidden" name="status" value="paused">
                    <button class="btn btn-outline-warning" type="submit">Pause season</button>
                </form>
            <?php elseif ($seasonRecord['status'] === 'paused'): ?>
                <form method="post" action="<?php echo e(base_url('app/modules/seasons/actions.php')); ?>" class="d-inline">
                    <?php echo csrf_input(); ?>
                    <input type="hidden" name="action" value="set_status">
                    <input type="hidden" name="id" value="<?php echo e((string) $seasonRecord['id']); ?>">
                    <input type="hidden" name="status" value="active">
                    <button class="btn btn-outline-jlfc" type="submit">Resume season</button>
                </form>
            <?php endif; ?>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/seasons/edit.php?id=' . (int) $seasonRecord['id'])); ?>">Edit</a>
            <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/seasons/delete.php?id=' . (int) $seasonRecord['id'])); ?>">Delete</a>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-calendar-event"></i>
                <div>
                    <h4>Start date</h4>
                    <p><?php echo e($seasonRecord['start_date']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-calendar-check"></i>
                <div>
                    <h4>End date</h4>
                    <p><?php echo e($seasonRecord['end_date']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-flag"></i>
                <div>
                    <h4>Status</h4>
                    <p><span class="badge <?php echo e(season_status_badge_class($seasonRecord['status'])); ?>"><?php echo e(ucfirst($seasonRecord['status'])); ?></span></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-pause-circle"></i>
                <div>
                    <h4>Paused at</h4>
                    <p><?php echo e($seasonRecord['paused_at'] ?: 'Not paused'); ?></p>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Competitions</p>
            <h3 class="h5 mb-0">Season competitions</h3>
        </div>
        <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/competitions/create.php?season_id=' . (int) $seasonRecord['id'])); ?>">Add competition</a>
    </div>
    <?php if ($competitions === []): ?>
        <p class="text-muted mb-0">No competitions are linked to this season yet.</p>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Type</th>
                        <th>Teams</th>
                        <th>Status</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($competitions as $competition): ?>
                        <tr>
                            <td><?php echo e($competition['name']); ?></td>
                            <td><?php echo e(ucfirst(str_replace('_', ' ', $competition['type']))); ?></td>
                            <td><?php echo e((string) $competition['number_of_teams']); ?></td>
                            <td><?php echo e(ucfirst($competition['status'])); ?></td>
                            <td class="text-end"><a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/competitions/view.php?id=' . (int) $competition['id'])); ?>">View</a></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>