<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

fixture_module_boot();

$fixtureRecord = find_fixture_or_abort((int) ($_GET['id'] ?? 0));
$fixtureCountdown = fixture_countdown($fixtureRecord['match_date'], $fixtureRecord['match_time']);
$formErrors = flash('errors') ?? [];
$pageTitle = 'View Fixture';

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">Fixture profile</p>
            <h2 class="h4 mb-0"><?php echo e($fixtureRecord['home_club_name']); ?> vs <?php echo e($fixtureRecord['away_club_name']); ?></h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/fixtures/edit.php?id=' . (int) $fixtureRecord['id'])); ?>">Edit</a>
            <?php if (can('submit_results') || can('verify_results')): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/matches/view.php?fixture_id=' . (int) $fixtureRecord['id'])); ?>">Match result</a>
            <?php endif; ?>
            <?php if (can('manage_protests')): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/protests/create.php?fixture_id=' . (int) $fixtureRecord['id'])); ?>">New protest</a>
            <?php endif; ?>
            <?php if (can('manage_media')): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/media/create-gallery.php?fixture_id=' . (int) $fixtureRecord['id'])); ?>">Add media</a>
            <?php endif; ?>
            <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/fixtures/delete.php?id=' . (int) $fixtureRecord['id'])); ?>">Delete</a>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-6">
            <div class="feature-tile"><i class="bi bi-trophy"></i>
                <div>
                    <h4>Competition</h4>
                    <p><?php echo e($fixtureRecord['competition_name']); ?> (<?php echo e($fixtureRecord['season_name']); ?>)</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-calendar-week"></i>
                <div>
                    <h4>Matchday</h4>
                    <p><?php echo e((string) $fixtureRecord['matchday']); ?></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(fixture_badge_class($fixtureRecord['status'])); ?>"><?php echo e(ucfirst($fixtureRecord['status'])); ?></span></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-diagram-3"></i>
                <div>
                    <h4>Stage</h4>
                    <p><?php echo e($fixtureRecord['group_label'] ?: ($fixtureRecord['knockout_round'] ?: ucwords(str_replace('_', ' ', (string) ($fixtureRecord['stage_type'] ?? 'league'))))); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="feature-tile"><i class="bi bi-hourglass-split"></i>
                <div>
                    <h4>Countdown</h4>
                    <?php if ($fixtureCountdown !== null && ($fixtureRecord['season_status'] ?? '') === 'active' && ($fixtureRecord['competition_status'] ?? '') === 'active' && $fixtureRecord['status'] === 'scheduled'): ?>
                        <p data-live-countdown data-deadline="<?php echo e($fixtureCountdown['deadline_iso']); ?>"><span data-live-countdown-value><?php echo e((string) $fixtureCountdown['days_remaining']); ?>d 00h 00m 00s</span></p>
                    <?php elseif (($fixtureRecord['season_status'] ?? '') === 'paused'): ?>
                        <p>Season paused</p>
                    <?php elseif (($fixtureRecord['competition_status'] ?? '') === 'paused'): ?>
                        <p>Competition paused</p>
                    <?php else: ?>
                        <p>Not active</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-calendar-event"></i>
                <div>
                    <h4>Date</h4>
                    <p><?php echo e($fixtureRecord['match_date'] ?: 'TBD'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-clock"></i>
                <div>
                    <h4>Time</h4>
                    <p><?php echo e(format_time_12h($fixtureRecord['match_time'] ?? null, 'Pending')); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-geo-alt"></i>
                <div>
                    <h4>Venue</h4>
                    <p><?php echo e($fixtureRecord['venue_name'] ?: 'Unassigned'); ?><?php echo $fixtureRecord['venue_location'] ? ' - ' . e($fixtureRecord['venue_location']) : ''; ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="feature-tile"><i class="bi bi-whistle"></i>
                <div>
                    <h4>Referee</h4>
                    <p><?php echo e($fixtureRecord['referee_name'] ?: 'Unassigned'); ?><?php echo $fixtureRecord['referee_level'] ? ' (' . e(ucfirst($fixtureRecord['referee_level'])) . ')' : ''; ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="feature-tile"><i class="bi bi-clipboard-check"></i>
                <div>
                    <h4>Match commissioner</h4>
                    <p><?php echo e($fixtureRecord['match_commissioner'] ?: 'Unassigned'); ?></p>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="row g-4 mt-1">
    <div class="col-12 col-lg-6">
        <article class="panel-card h-100">
            <div class="panel-card-header mb-3">
                <div>
                    <p class="eyebrow mb-1">Postponement</p>
                    <h3 class="h5 mb-0">Postpone fixture and publish notice</h3>
                </div>
            </div>
            <form method="post" action="<?php echo e(base_url('app/modules/fixtures/actions.php')); ?>">
                <?php echo csrf_input(); ?>
                <input type="hidden" name="action" value="postpone">
                <input type="hidden" name="id" value="<?php echo e((string) $fixtureRecord['id']); ?>">
                <div class="mb-3">
                    <label class="form-label" for="postponement_reason">Reason for postponement</label>
                    <textarea class="form-control <?php echo validation_error($formErrors, 'postponement_reason') ? 'is-invalid' : ''; ?>" id="postponement_reason" name="postponement_reason" rows="4" placeholder="Weather, security, venue issue, or any official reason."><?php echo e((string) old('postponement_reason', (string) ($fixtureRecord['postponement_reason'] ?? ''))); ?></textarea>
                    <?php if (validation_error($formErrors, 'postponement_reason')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'postponement_reason')); ?></div><?php endif; ?>
                </div>
                <button class="btn btn-outline-warning" type="submit">Postpone and publish news</button>
            </form>
        </article>
    </div>
    <div class="col-12 col-lg-6">
        <article class="panel-card h-100">
            <div class="panel-card-header mb-3">
                <div>
                    <p class="eyebrow mb-1">Reschedule</p>
                    <h3 class="h5 mb-0">Set a new date and publish update</h3>
                </div>
            </div>
            <form method="post" action="<?php echo e(base_url('app/modules/fixtures/actions.php')); ?>">
                <?php echo csrf_input(); ?>
                <input type="hidden" name="action" value="reschedule">
                <input type="hidden" name="id" value="<?php echo e((string) $fixtureRecord['id']); ?>">
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label" for="reschedule_match_date">New match date</label>
                        <input class="form-control <?php echo validation_error($formErrors, 'match_date') ? 'is-invalid' : ''; ?>" id="reschedule_match_date" name="match_date" type="date" value="<?php echo e((string) old('match_date', (string) ($fixtureRecord['match_date'] ?? ''))); ?>">
                        <?php if (validation_error($formErrors, 'match_date')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'match_date')); ?></div><?php endif; ?>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label" for="reschedule_match_time">New kickoff time</label>
                        <input class="form-control <?php echo validation_error($formErrors, 'match_time') ? 'is-invalid' : ''; ?>" id="reschedule_match_time" name="match_time" type="time" value="<?php echo e((string) old('match_time', (string) ($fixtureRecord['match_time'] ?? ''))); ?>">
                        <?php if (validation_error($formErrors, 'match_time')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'match_time')); ?></div><?php endif; ?>
                    </div>
                    <div class="col-12">
                        <label class="form-label" for="reschedule_reason">Reschedule note</label>
                        <textarea class="form-control <?php echo validation_error($formErrors, 'postponement_reason') ? 'is-invalid' : ''; ?>" id="reschedule_reason" name="postponement_reason" rows="4" placeholder="Explain why the original fixture moved and confirm the new schedule."><?php echo e((string) old('postponement_reason', (string) ($fixtureRecord['postponement_reason'] ?? ''))); ?></textarea>
                        <?php if (validation_error($formErrors, 'postponement_reason')): ?><div class="invalid-feedback"><?php echo e((string) validation_error($formErrors, 'postponement_reason')); ?></div><?php endif; ?>
                    </div>
                </div>
                <?php if (!empty($fixtureRecord['rescheduled_from_date'])): ?>
                    <p class="small text-muted mt-3 mb-0">Previous schedule: <?php echo e((string) $fixtureRecord['rescheduled_from_date']); ?><?php echo !empty($fixtureRecord['rescheduled_from_time']) ? ' ' . e(format_time_12h((string) $fixtureRecord['rescheduled_from_time'], '')) : ''; ?></p>
                <?php endif; ?>
                <button class="btn btn-outline-jlfc mt-3" type="submit">Reschedule and publish news</button>
            </form>
        </article>
    </div>
</section>

<section class="row g-4">
    <div class="col-12 col-lg-6">
        <article class="panel-card h-100">
            <div class="panel-card-header mb-3">
                <div>
                    <p class="eyebrow mb-1">Teams</p>
                    <h3 class="h5 mb-0">Match pairing</h3>
                </div>
            </div>
            <div class="row g-3">
                <div class="col-md-6">
                    <div class="feature-tile"><i class="bi bi-house-door"></i>
                        <div>
                            <h4>Home club</h4>
                            <p><?php echo e($fixtureRecord['home_club_name']); ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="feature-tile"><i class="bi bi-airplane"></i>
                        <div>
                            <h4>Away club</h4>
                            <p><?php echo e($fixtureRecord['away_club_name']); ?></p>
                        </div>
                    </div>
                </div>
            </div>
        </article>
    </div>
    <div class="col-12 col-lg-6">
        <article class="panel-card h-100">
            <div class="panel-card-header mb-3">
                <div>
                    <p class="eyebrow mb-1">Result status</p>
                    <h3 class="h5 mb-0">Recorded match result</h3>
                </div>
            </div>
            <?php if ($fixtureRecord['result_id'] !== null): ?>
                <div class="feature-tile">
                    <i class="bi bi-stopwatch"></i>
                    <div>
                        <h4><?php echo e((string) $fixtureRecord['home_score']); ?> - <?php echo e((string) $fixtureRecord['away_score']); ?></h4>
                        <p><?php echo e(ucfirst((string) $fixtureRecord['result_status'])); ?> result on file.</p>
                    </div>
                </div>
                <?php if (can('submit_results') || can('verify_results')): ?>
                    <div class="mt-3">
                        <a class="btn btn-outline-jlfc btn-sm" href="<?php echo e(base_url('app/modules/matches/view.php?fixture_id=' . (int) $fixtureRecord['id'])); ?>">Open result workflow</a>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <p class="text-muted mb-0">No result has been recorded for this fixture yet.</p>
                <?php if (can('submit_results') || can('verify_results')): ?>
                    <div class="mt-3">
                        <a class="btn btn-outline-jlfc btn-sm" href="<?php echo e(base_url('app/modules/matches/view.php?fixture_id=' . (int) $fixtureRecord['id'])); ?>">Submit result</a>
                    </div>
                <?php endif; ?>
            <?php endif; ?>
        </article>
    </div>
</section>

<section class="panel-card mt-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Officiating notes</p>
            <h3 class="h5 mb-0">Supporting officials</h3>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-person-lines-fill"></i>
                <div>
                    <h4>Assistant referee 1</h4>
                    <p><?php echo e($fixtureRecord['assistant_referee_1'] ?: 'Unassigned'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-person-lines-fill"></i>
                <div>
                    <h4>Assistant referee 2</h4>
                    <p><?php echo e($fixtureRecord['assistant_referee_2'] ?: 'Unassigned'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-person-badge"></i>
                <div>
                    <h4>Fourth official</h4>
                    <p><?php echo e($fixtureRecord['fourth_official'] ?: 'Unassigned'); ?></p>
                </div>
            </div>
        </div>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>