@extends('layouts.app')

@section('title', ($activeCategory->name ?? 'Shop All') . ' — Natural Skincare')

@section('content')

    <x-breadcrumb :items="array_filter([['label' => 'Shop', 'url' => route('shop')], $activeCategory ? ['label' => $activeCategory->name] : null])" />

    {{-- Page header --}}
    <section class="bg-cream-100 py-12 text-center lg:py-16">
        <div class="container-shop">
            <p class="eyebrow mb-2">{{ config('shop.tagline') }}</p>
            <h1 class="font-serif text-3xl font-medium text-ink-900 sm:text-4xl">{{ $activeCategory->name ?? 'Shop All Skincare' }}</h1>
            <p class="mx-auto mt-3 max-w-xl text-sm leading-relaxed text-ink-500">
                {{ $activeCategory->description ?? 'Small-batch skincare and haircare, made with farm-fresh ingredients and zero nasties.' }}
            </p>
        </div>
    </section>

    <section class="py-12 lg:py-16">
        <div class="container-shop grid gap-10 lg:grid-cols-[250px_1fr]">

            {{-- Filters sidebar --}}
            <aside class="hidden lg:block" aria-label="Filters">
                <div class="sticky top-28 space-y-8">
                    <!-- Search input -->
                    <div>
                        <h2 class="mb-3 border-b border-cream-200 pb-3 text-xs font-semibold tracking-[0.2em] uppercase">Search Catalogue</h2>
                        <form action="{{ route('shop', $activeCategory->slug ?? null) }}" method="GET" class="flex gap-2">
                            <input type="text" name="search" value="{{ $search }}" placeholder="Search products..." class="w-full border border-sand-300 px-3 py-2 text-xs focus:border-moss-600 focus:outline-none">
                            <button type="submit" class="btn-primary !px-3 !py-2 !text-xs">Go</button>
                        </form>
                    </div>

                    <div>
                        <h2 class="mb-4 border-b border-cream-200 pb-3 text-xs font-semibold tracking-[0.2em] uppercase">Categories</h2>
                        <ul class="space-y-2.5 text-sm text-ink-700">
                            <li>
                                <a href="{{ route('shop') }}" class="flex items-center justify-between transition-colors hover:text-moss-600 {{ !$activeCategory ? 'font-semibold text-moss-700' : '' }}">
                                    All Products
                                </a>
                            </li>
                            @foreach ($categories as $category)
                                <li>
                                    <a href="{{ route('shop', $category->slug) }}" class="flex items-center justify-between transition-colors hover:text-moss-600 {{ ($activeCategory->slug ?? null) === $category->slug ? 'font-semibold text-moss-700' : '' }}">
                                        {{ $category->name }}
                                    </a>
                                </li>
                            @endforeach
                        </ul>
                    </div>
                </div>
            </aside>

            {{-- Product grid --}}
            <div>
                <div class="mb-8 flex flex-wrap items-center justify-between gap-4 border-b border-cream-200 pb-4">
                    <p class="text-sm text-ink-500">Showing <span class="font-semibold text-ink-900">{{ $products->count() }}</span> products</p>
                    
                    <form action="{{ route('shop', $activeCategory->slug ?? null) }}" method="GET" class="flex items-center gap-3">
                        @if($search)
                            <input type="hidden" name="search" value="{{ $search }}">
                        @endif
                        <label for="sort" class="hidden text-xs tracking-wider text-ink-500 uppercase sm:block">Sort by</label>
                        <select name="sort" id="sort" onchange="this.form.submit()" class="cursor-pointer border border-sand-300 bg-white px-3 py-2 text-sm focus:border-moss-600 focus:ring-1 focus:ring-moss-600 focus:outline-none">
                            <option value="newest" {{ $sort === 'newest' ? 'selected' : '' }}>Newest Arrivals</option>
                            <option value="price_low" {{ $sort === 'price_low' ? 'selected' : '' }}>Price: Low to High</option>
                            <option value="price_high" {{ $sort === 'price_high' ? 'selected' : '' }}>Price: High to Low</option>
                            <option value="rating" {{ $sort === 'rating' ? 'selected' : '' }}>Top Rated</option>
                        </select>
                    </form>
                </div>

                @if($products->isEmpty())
                    <div class="py-16 text-center">
                        <p class="font-serif text-xl text-ink-900">No products found matching your search</p>
                        <p class="mt-2 text-sm text-ink-500">Try clearing filters or searching for something else.</p>
                        <a href="{{ route('shop') }}" class="btn-outline mt-6">View All Products</a>
                    </div>
                @else
                    <div class="grid grid-cols-2 gap-x-5 gap-y-10 md:grid-cols-3">
                        @foreach ($products as $product)
                            <x-product-card :product="$product" />
                        @endforeach
                    </div>
                @endif
            </div>
        </div>
    </section>

@endsection
