<?php

declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';

$mediaId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$item = null;
$relatedItems = [];

if ($mediaId > 0) {
    try {
        $item = db_fetch_one(
            'SELECT mg.id, mg.title, mg.file_path, mg.media_type, mg.created_at, mg.fixture_id, mg.club_id,
                    f.matchday, f.match_date,
                    hc.name AS home_club_name, ac.name AS away_club_name,
                    c.name AS club_name
             FROM media_gallery mg
             LEFT JOIN fixtures f ON f.id = mg.fixture_id
             LEFT JOIN clubs hc ON hc.id = f.home_club_id
             LEFT JOIN clubs ac ON ac.id = f.away_club_id
             LEFT JOIN clubs c ON c.id = mg.club_id
             WHERE mg.id = :id
             LIMIT 1',
            ['id' => $mediaId]
        );

        if ($item !== null) {
            $relatedItems = db_fetch_all(
                'SELECT id, title, media_type, file_path, created_at
                 FROM media_gallery
                 WHERE id <> :id
                 ORDER BY created_at DESC, id DESC
                 LIMIT 4',
                ['id' => $mediaId]
            );
        }
    } catch (Throwable) {
    }
}

$pageTitle = $item['title'] ?? 'Gallery Item';
$lead = $item !== null
    ? 'Match and club media published by the JLFC media desk.'
    : 'Match visuals, club media, and league content curated from the JLFC media desk.';

render_public_page($pageTitle, $lead, static function () use ($item, $relatedItems): void {
?>
    <section class="panel-card mb-4">
        <?php if ($item === null): ?>
            <p class="text-muted mb-3">The requested media item could not be found.</p>
            <a class="btn btn-outline-jlfc btn-sm" href="<?php echo e(base_url('app/public/gallery.php')); ?>">Back to gallery</a>
        <?php else: ?>
            <p class="eyebrow mb-2"><?php echo e(date('d M Y', strtotime((string) $item['created_at']))); ?></p>
            <h2 class="h3 mb-3"><?php echo e($item['title']); ?></h2>
            <p class="text-muted mb-4">
                <?php echo e($item['fixture_id'] ? (($item['home_club_name'] ?? '') . ' vs ' . ($item['away_club_name'] ?? '')) : 'General league media'); ?>
                <?php echo $item['club_name'] ? ' · ' . e((string) $item['club_name']) : ''; ?>
            </p>
            <?php if (($item['media_type'] ?? '') === 'video'): ?>
                <video class="w-100 rounded-4 mb-4" controls src="<?php echo e(base_url((string) $item['file_path'])); ?>"></video>
            <?php else: ?>
                <img class="img-fluid rounded-4 mb-4" src="<?php echo e(base_url((string) $item['file_path'])); ?>" alt="<?php echo e($item['title']); ?>">
            <?php endif; ?>
            <a class="btn btn-outline-jlfc btn-sm" href="<?php echo e(base_url('app/public/gallery.php')); ?>">Back to gallery</a>
        <?php endif; ?>
    </section>

    <?php if ($item !== null): ?>
        <section class="panel-card">
            <div class="panel-card-header mb-3">
                <div>
                    <p class="eyebrow mb-1">Gallery</p>
                    <h3 class="h5 mb-0">More media</h3>
                </div>
            </div>
            <?php if ($relatedItems === []): ?>
                <p class="text-muted mb-0">No other media items are available yet.</p>
            <?php else: ?>
                <div class="row g-3">
                    <?php foreach ($relatedItems as $relatedItem): ?>
                        <div class="col-md-6 col-xl-3">
                            <article class="panel-card h-100">
                                <?php if (($relatedItem['media_type'] ?? '') === 'video'): ?>
                                    <video class="w-100 rounded-4 mb-3" controls src="<?php echo e(base_url((string) $relatedItem['file_path'])); ?>"></video>
                                <?php else: ?>
                                    <img class="img-fluid rounded-4 mb-3" src="<?php echo e(base_url((string) $relatedItem['file_path'])); ?>" alt="<?php echo e($relatedItem['title']); ?>">
                                <?php endif; ?>
                                <p class="eyebrow mb-2"><?php echo e(date('d M Y', strtotime((string) $relatedItem['created_at']))); ?></p>
                                <h4 class="h6 mb-3"><?php echo e($relatedItem['title']); ?></h4>
                                <a class="btn btn-outline-jlfc btn-sm" href="<?php echo e(base_url('app/public/gallery-item.php?id=' . (int) $relatedItem['id'])); ?>">Open media</a>
                            </article>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </section>
    <?php endif; ?>
<?php
});
