<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

competition_module_boot();

$competitionRecord = find_competition_or_abort((int) ($_GET['id'] ?? 0));
$fixtureCount = (int) db_fetch_value('SELECT COUNT(*) FROM fixtures WHERE competition_id = :competition_id', ['competition_id' => $competitionRecord['id']], 0);
$pageTitle = 'View Competition';

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">Competition profile</p>
            <h2 class="h4 mb-0"><?php echo e($competitionRecord['name']); ?></h2>
        </div>
        <div class="d-flex gap-2 flex-wrap">
            <?php if ($competitionRecord['status'] === 'active'): ?>
                <form method="post" action="<?php echo e(base_url('app/modules/competitions/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) $competitionRecord['id']); ?>">
                    <input type="hidden" name="status" value="paused">
                    <button class="btn btn-outline-warning" type="submit">Pause competition</button>
                </form>
            <?php else: ?>
                <form method="post" action="<?php echo e(base_url('app/modules/competitions/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) $competitionRecord['id']); ?>">
                    <input type="hidden" name="status" value="active">
                    <button class="btn btn-outline-jlfc" type="submit"><?php echo $competitionRecord['status'] === 'paused' ? 'Resume competition' : 'Set active'; ?></button>
                </form>
            <?php endif; ?>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/competitions/edit.php?id=' . (int) $competitionRecord['id'])); ?>">Edit</a>
            <?php if (can('manage_awards')): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/awards/create.php?season_id=' . (int) $competitionRecord['season_id'] . '&competition_id=' . (int) $competitionRecord['id'])); ?>">Add award</a>
            <?php endif; ?>
            <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/competitions/delete.php?id=' . (int) $competitionRecord['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>Season</h4>
                    <p><?php echo e($competitionRecord['season_name']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-diagram-3"></i>
                <div>
                    <h4>Type</h4>
                    <p><?php echo e(ucwords(str_replace('_', ' ', $competitionRecord['type']))); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-signpost-split"></i>
                <div>
                    <h4>Rotation order</h4>
                    <p><?php echo e((string) $competitionRecord['sequence_order']); ?></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(competition_status_badge_class($competitionRecord['status'])); ?>"><?php echo e(ucfirst($competitionRecord['status'])); ?></span></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-people"></i>
                <div>
                    <h4>Teams</h4>
                    <p><?php echo e((string) $competitionRecord['number_of_teams']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-diagram-2"></i>
                <div>
                    <h4>Group format</h4>
                    <p><?php echo e((string) ($competitionRecord['group_count'] ?: 0)); ?> groups · <?php echo e((string) ($competitionRecord['qualifiers_per_group'] ?: 0)); ?> qualifiers</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-award"></i>
                <div>
                    <h4>Points</h4>
                    <p>W <?php echo e((string) $competitionRecord['points_win']); ?> / D <?php echo e((string) $competitionRecord['points_draw']); ?> / L <?php echo e((string) $competitionRecord['points_loss']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-bar-chart"></i>
                <div>
                    <h4>Tie breaker</h4>
                    <p><?php echo e(ucwords(str_replace('_', ' ', $competitionRecord['tie_breaker_type']))); ?></p>
                </div>
            </div>
        </div>
    </div>
</section>
<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Workflow state</p>
            <h3 class="h5 mb-0">Competition readiness</h3>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-6">
            <div class="feature-tile"><i class="bi bi-calendar-week"></i>
                <div>
                    <h4>Fixtures linked</h4>
                    <p><?php echo e((string) $fixtureCount); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="feature-tile"><i class="bi bi-calendar-range"></i>
                <div>
                    <h4>Parent season status</h4>
                    <p><?php echo e(ucfirst($competitionRecord['season_status'])); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="feature-tile"><i class="bi bi-pause-circle"></i>
                <div>
                    <h4>Competition operation</h4>
                    <p><?php echo e($competitionRecord['status'] === 'paused' ? 'Paused while another competition can continue.' : 'Available for the current season workflow.'); ?></p>
                </div>
            </div>
        </div>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>