<?php
require_once __DIR__ . '/includes/config.php';

$categorySlug = $_GET['category'] ?? '';
$brandSlug    = $_GET['brand']    ?? '';

$category = $categorySlug ? getCategoryBySlug($categorySlug) : null;
$brand    = $brandSlug    ? getBrandBySlug($brandSlug) : null;

$filters = [];
if ($category) $filters['category_slug'] = $category['slug'];
if ($brand)    $filters['brand_slug']    = $brand['slug'];

$products = getProducts($filters);

// ── SEO ─────────────────────────────────────────────────────────────────────────
if ($category) {
    $pageTitle       = $category['name'] . ' - Premium Dirt Bike Parts | ' . SITE_NAME;
    $metaDescription = 'Shop premium aftermarket ' . strtolower($category['name']) . ' for dirt bikes at ' . SITE_NAME . '. Exact year-make-model fitment guaranteed. Free shipping over $99.';
    $canonicalUrl    = siteUrl('catalog.php?category=' . urlencode($category['slug']));
    $heading         = $category['name'];
    $subtitle        = 'Browse our complete selection of premium aftermarket ' . strtolower($category['name']) . ' to keep your dirt bike competitive.';
} elseif ($brand) {
    $pageTitle       = $brand['name'] . ' Dirt Bike Parts | ' . SITE_NAME;
    $metaDescription = 'Shop all ' . $brand['name'] . ' aftermarket dirt bike parts at ' . SITE_NAME . '. Exact fitment guaranteed. Free shipping over $99.';
    $canonicalUrl    = siteUrl('catalog.php?brand=' . urlencode($brand['slug']));
    $heading         = $brand['name'] . ' Parts';
    $subtitle        = 'Explore all factory and aftermarket dirt bike products directly engineered by ' . $brand['name'] . '.';
} else {
    $pageTitle       = 'Complete Catalog | ' . SITE_NAME;
    $metaDescription = 'Browse the complete ' . SITE_NAME . ' dirt bike parts catalog.';
    $canonicalUrl    = siteUrl('catalog.php');
    $heading         = 'Complete Catalog';
    $subtitle        = 'Browse our full inventory of premium aftermarket dirt bike parts with guaranteed exact fitment.';
}

require_once __DIR__ . '/includes/header.php';
?>

<header class="bg-black py-16 border-b border-gray-800">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center text-white">
        <h1 class="text-5xl md:text-6xl font-black uppercase tracking-tighter mb-4"><?= e($heading) ?></h1>
        <p class="text-xl text-gray-400 font-semibold max-w-2xl mx-auto"><?= e($subtitle) ?></p>
    </div>
</header>

<section class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-16" aria-label="Product Listing">
    <?php if (!empty($products)): ?>
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-8">
            <?php foreach ($products as $product): ?>
                <article class="group block bg-black border border-gray-800 rounded-2xl overflow-hidden hover:border-brand-orange transition-all duration-300 shadow-xl relative">
                    <a href="/product.php?slug=<?= urlencode($product['slug']) ?>" class="block">
                        <?php if ($product['discount_price'] < $product['msrp_price']): ?>
                            <div class="absolute top-4 right-4 bg-brand-red text-white text-xs font-black px-2 py-1 rounded tracking-wider z-10">SALE</div>
                        <?php endif; ?>
                        <div class="aspect-square bg-gray-900 p-6 relative overflow-hidden flex items-center justify-center">
                            <?php if ($product['image_url']): ?>
                                <img src="<?= e($product['image_url']) ?>" alt="<?= e($product['name']) ?> by <?= e($product['brand_name']) ?>" class="w-full h-full object-contain filter drop-shadow-2xl group-hover:scale-110 transition-transform duration-500" loading="lazy" width="400" height="400">
                            <?php endif; ?>
                        </div>
                        <div class="p-6">
                            <p class="text-brand-orange text-xs font-bold tracking-widest uppercase mb-2"><?= e($product['brand_name']) ?></p>
                            <h3 class="text-white font-bold mb-3 truncate" title="<?= e($product['name']) ?>"><?= e($product['name']) ?></h3>
                            <div class="flex items-end justify-between">
                                <div>
                                    <p class="text-gray-500 text-sm line-through decoration-brand-red font-semibold">$<?= number_format($product['msrp_price'], 2) ?></p>
                                    <p class="text-2xl font-black text-white">$<?= number_format($product['discount_price'], 2) ?></p>
                                </div>
                            </div>
                        </div>
                    </a>
                </article>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="text-center py-20">
            <svg class="mx-auto h-16 w-16 text-gray-600 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 002-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path></svg>
            <h3 class="text-2xl font-bold text-white mb-2">No Parts Found</h3>
            <p class="text-gray-400 font-semibold">Try adjusting your filters or browse a different category.</p>
            <a href="/" class="mt-6 inline-flex items-center text-brand-orange font-bold hover:text-white transition-colors">Back to Store <svg class="ml-2 w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14 5l7 7m0 0l-7 7m7-7H3"></path></svg></a>
        </div>
    <?php endif; ?>
</section>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
