<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

venue_module_boot();

$venueRecord = find_venue_or_abort((int) ($_GET['id'] ?? 0));
$linkedFixtures = db_fetch_all(
    'SELECT f.id, f.matchday, f.match_date, hc.name AS home_club, ac.name AS away_club, c.name AS competition_name
     FROM fixtures f
     INNER JOIN clubs hc ON hc.id = f.home_club_id
     INNER JOIN clubs ac ON ac.id = f.away_club_id
     INNER JOIN competitions c ON c.id = f.competition_id
     WHERE f.venue_id = :venue_id
     ORDER BY f.match_date DESC, f.id DESC
     LIMIT 10',
    ['venue_id' => $venueRecord['id']]
);
$pageTitle = 'View Venue';

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">Venue profile</p>
            <h2 class="h4 mb-0"><?php echo e($venueRecord['name']); ?></h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/venues/edit.php?id=' . (int) $venueRecord['id'])); ?>">Edit</a>
            <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/venues/delete.php?id=' . (int) $venueRecord['id'])); ?>">Delete</a>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-geo-alt"></i>
                <div>
                    <h4>Location</h4>
                    <p><?php echo e($venueRecord['location']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-people"></i>
                <div>
                    <h4>Capacity</h4>
                    <p><?php echo e((string) ($venueRecord['capacity'] ?? 0)); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-grid-3x3-gap"></i>
                <div>
                    <h4>Pitch type</h4>
                    <p><?php echo e($venueRecord['pitch_type'] ?: 'Not set'); ?></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(venue_badge_class($venueRecord['status'])); ?>"><?php echo e(ucwords(str_replace('_', ' ', $venueRecord['status']))); ?></span></p>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Linked fixtures</p>
            <h3 class="h5 mb-0">Recent venue usage</h3>
        </div>
    </div>
    <?php if ($linkedFixtures === []): ?>
        <p class="text-muted mb-0">No fixtures are linked to this venue yet.</p>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Competition</th>
                        <th>Fixture</th>
                        <th>Date</th>
                        <th>Matchday</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($linkedFixtures as $fixture): ?>
                        <tr>
                            <td><?php echo e($fixture['competition_name']); ?></td>
                            <td><?php echo e($fixture['home_club']); ?> vs <?php echo e($fixture['away_club']); ?></td>
                            <td><?php echo e($fixture['match_date'] ?: 'TBD'); ?></td>
                            <td><?php echo e((string) $fixture['matchday']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>