<?php
require __DIR__ . '/config.php';

$selectedCategory = trim((string) ($_GET['category'] ?? ''));
$photos = public_photos($selectedCategory, 72);
$heroPhoto = featured_photo();
$heroShape = $heroPhoto ? photo_shape_class($heroPhoto) : 'shape-landscape';
$shoots = array_values(array_filter(public_shoots(), function (array $shoot): bool {
    return (int) ($shoot['photo_count'] ?? 0) > 0;
}));

page_header('Home', 'portfolio-page');
?>
<section class="hero hero-<?= e(str_replace('shape-', '', $heroShape)) ?>">
    <?php if ($heroPhoto): ?>
        <div class="hero-media">
            <?= media_preview($heroPhoto, 2800, [900, 1400, 2000, 2800], '100vw', ['loading' => 'eager', 'decoding' => 'async', 'fetchpriority' => 'high']) ?>
        </div>
    <?php endif; ?>
    <div class="hero-copy">
        <p class="eyebrow">Photography</p>
        <h1>Oli Peirson</h1>
        <p class="hero-placeholder"></p>
        <div class="hero-actions">
            <a class="button" href="#portfolio">View more</a>
        </div>
    </div>
</section>

<?php if ($shoots): ?>
    <section class="album-showcase" aria-labelledby="albums-heading">
        <div class="section-heading shoot-heading">
            <div>
                <p class="eyebrow">Collections</p>
                <h2 id="albums-heading">Albums</h2>
            </div>
        </div>

        <div class="shoot-carousel" data-carousel>
            <button class="carousel-button carousel-button-prev" type="button" data-carousel-prev aria-label="Previous album">
                <span aria-hidden="true">&lsaquo;</span>
            </button>
            <section class="shoot-grid" aria-label="Photography albums" data-carousel-track>
                <?php foreach ($shoots as $index => $shoot): ?>
                    <article class="shoot-card">
                        <a href="/shoot.php?slug=<?= urlencode((string) $shoot['slug']) ?>">
                            <?php if (!empty($shoot['cover_image'])): ?>
                                <?php $coverPhoto = ['image_path' => $shoot['cover_image'], 'media_type' => $shoot['cover_media_type'] ?? 'image', 'alt_text' => $shoot['cover_alt']]; ?>
                                <?= media_preview($coverPhoto, 900, [480, 900, 1400], '(max-width: 720px) 82vw, 420px', ['loading' => 'lazy', 'decoding' => 'async']) ?>
                            <?php endif; ?>
                            <div>
                                <span><?= e(str_pad((string) ($index + 1), 2, '0', STR_PAD_LEFT)) ?></span>
                                <h2><?= e($shoot['title']) ?></h2>
                                <p><?= e((string) $shoot['photo_count']) ?> file<?= (int) $shoot['photo_count'] === 1 ? '' : 's' ?></p>
                            </div>
                        </a>
                    </article>
                <?php endforeach; ?>
            </section>
            <button class="carousel-button carousel-button-next" type="button" data-carousel-next aria-label="Next album">
                <span aria-hidden="true">&rsaquo;</span>
            </button>
        </div>
    </section>
<?php endif; ?>

<?php if ($photos): ?>
    <section id="portfolio" class="photo-grid" aria-label="Photography gallery">
        <?php foreach ($photos as $photo): ?>
            <article class="photo-card layout-<?= e($photo['layout'] ?? 'standard') ?> <?= e(photo_shape_class($photo)) ?>">
                <a class="lightbox-trigger" href="<?= e(photo_url($photo)) ?>" data-full-src="<?= e(lightbox_photo_url($photo)) ?>" data-full-srcset="<?= e(lightbox_srcset($photo)) ?>" data-media-type="<?= e(media_type($photo)) ?>" data-title="<?= e($photo['title']) ?>" data-meta="<?= e(trim((string) ($photo['location'] ?? '') . ' ' . (string) ($photo['shot_date'] ?? ''))) ?>">
                    <?= media_preview($photo, 900, [480, 900, 1400], '(max-width: 720px) 100vw, (max-width: 1200px) 50vw, 34vw', ['loading' => 'lazy', 'decoding' => 'async']) ?>
                </a>
            </article>
        <?php endforeach; ?>
    </section>
<?php else: ?>
    <section class="empty-state">
        <p class="eyebrow">Ready for your first upload</p>
        <h2>No work added yet</h2>
        <p>Log in to the admin area and add your first photo or video. It will appear here automatically.</p>
        <a class="button" href="/admin.php">Open admin</a>
    </section>
<?php endif; ?>

<?php
page_footer();
