@extends('layouts.app')

@php
    use Illuminate\Support\Str;
@endphp

@section('content')

<div class="bg-gray-50 min-h-screen">

    <div class="container mx-auto px-4 sm:px-6 lg:px-8 py-6">

        <nav class="flex items-center text-sm text-gray-500 mb-6 space-x-2">
            <a href="{{ route('home') }}" class="hover:text-emerald-600 transition-colors">Home</a>
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
            <span class="text-gray-900 font-medium">{{ $category->name }}</span>
        </nav>

        <div class="flex flex-col lg:flex-row gap-8">

            <aside class="w-full lg:w-72 shrink-0">
                <div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
                    <div class="px-4 sm:px-6 py-4 bg-gradient-to-r from-emerald-600 to-emerald-500">
                        <h3 class="text-white font-semibold text-sm flex items-center gap-2">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 10h16M4 14h16M4 18h16"/></svg>
                            Subcategories
                        </h3>
                    </div>
                    <div class="py-2">
                        @forelse($category->subcategories as $subcategory)
                            <a href="{{ route('subcategory.show', ['category' => $category->slug, 'subcategory' => $subcategory->slug]) }}" class="flex items-center justify-between px-4 sm:px-6 py-3 text-sm text-gray-600 hover:bg-emerald-50 hover:text-emerald-700 transition-all group">
                                <span class="flex items-center gap-3 min-w-0 truncate">
                                    <span class="w-1.5 h-1.5 rounded-full bg-emerald-300 group-hover:bg-emerald-500 transition-colors shrink-0"></span>
                                    <span class="truncate">{{ $subcategory->name }}</span>
                                </span>
                                <svg class="w-4 h-4 text-gray-300 group-hover:text-emerald-500 group-hover:translate-x-0.5 transition-all shrink-0 ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
                            </a>
                        @empty
                            <p class="px-6 py-4 text-sm text-gray-400 italic">No subcategories yet</p>
                        @endforelse
                    </div>
                </div>

                <div class="bg-white rounded-2xl shadow-sm border border-gray-100 mt-6 p-6">
                    <h4 class="text-sm font-semibold text-gray-800 mb-3">Need Help?</h4>
                    <p class="text-xs text-gray-500 leading-relaxed">Contact our support team for any questions about this category.</p>
                    <a href="{{ route('contact') }}" class="mt-4 inline-flex items-center gap-1.5 text-xs font-medium text-emerald-600 hover:text-emerald-700 transition-colors">
                        Get in Touch
                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3"/></svg>
                    </a>
                </div>
            </aside>

            <main class="flex-1 min-w-0">

                <div class="bg-white rounded-2xl shadow-sm border border-gray-100 p-6 mb-6">
                    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                        <div>
                            <h1 class="text-2xl font-bold text-gray-900">{{ $category->name }}</h1>
                            @if($category->description)
                                <p class="text-sm text-gray-500 mt-1">{{ $category->description }}</p>
                            @endif
                            <p class="text-xs text-gray-400 mt-2">{{ $products->total() }} {{ Str::plural('product', $products->total()) }} found</p>
                        </div>
                        <div class="flex flex-col sm:flex-row items-stretch sm:items-center gap-2 sm:gap-3 w-full sm:w-auto">
                            <form action="{{ request()->url() }}" method="GET" class="flex items-center gap-2 flex-1 min-w-0">
                                <div class="relative flex-1 min-w-0">
                                    <svg class="w-4 h-4 text-gray-400 absolute left-3 top-1/2 -translate-y-1/2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/></svg>
                                    <input type="text" name="search" value="{{ request('search') }}" placeholder="Search..." class="text-sm border border-gray-200 rounded-xl pl-9 pr-4 py-2.5 bg-gray-50 focus:bg-white focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500 outline-none transition-all w-full min-w-0">
                                </div>
                                <input type="hidden" name="sort" value="{{ request('sort', 'featured') }}">
                                <button type="submit" class="text-sm px-3 sm:px-4 py-2.5 bg-emerald-600 text-white rounded-xl hover:bg-emerald-700 transition-all flex-shrink-0">Search</button>
                            </form>
                            <select onchange="window.location.href=this.value" class="text-sm border border-gray-200 rounded-xl px-3 sm:px-4 py-2.5 bg-white focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500 outline-none transition-all cursor-pointer w-full sm:w-auto">
                                <option value="{{ request()->url() }}?sort=featured{{ request('search') ? '&search='.request('search') : '' }}" {{ request('sort', 'featured') === 'featured' ? 'selected' : '' }}>Featured</option>
                                <option value="{{ request()->url() }}?sort=price_asc{{ request('search') ? '&search='.request('search') : '' }}" {{ request('sort') === 'price_asc' ? 'selected' : '' }}>Price: Low to High</option>
                                <option value="{{ request()->url() }}?sort=price_desc{{ request('search') ? '&search='.request('search') : '' }}" {{ request('sort') === 'price_desc' ? 'selected' : '' }}>Price: High to Low</option>
                                <option value="{{ request()->url() }}?sort=newest{{ request('search') ? '&search='.request('search') : '' }}" {{ request('sort') === 'newest' ? 'selected' : '' }}>Newest</option>
                                <option value="{{ request()->url() }}?sort=name{{ request('search') ? '&search='.request('search') : '' }}" {{ request('sort') === 'name' ? 'selected' : '' }}>Name: A to Z</option>
                            </select>
                        </div>
                    </div>
                </div>

                @if($products->count() > 0)
                    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 gap-5">
                        @foreach($products as $product)
                            @php
                                $hasSale = $product->sale_price && now()->between($product->sale_price_start_date, $product->sale_price_end_date);
                                $discount = $hasSale ? round((($product->price - $product->sale_price) / $product->price) * 100) : 0;
                            @endphp
                            <div class="group bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden hover:shadow-lg hover:-translate-y-1 transition-all duration-300">

                                <div class="relative aspect-square bg-gradient-to-br from-gray-50 to-gray-100 overflow-hidden">
                                    @if($product->images->first())
                                        <img src="{{ asset('storage/' . $product->images->first()->path) }}" alt="{{ $product->name }}" class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500">
                                    @else
                                        <div class="w-full h-full flex items-center justify-center">
                                            <svg class="w-16 h-16 text-gray-200" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                                        </div>
                                    @endif

                                    @if($hasSale)
                                        <span class="absolute top-3 left-3 bg-red-500 text-white text-xs font-bold px-2.5 py-1 rounded-lg shadow-sm">
                                            -{{ $discount }}%
                                        </span>
                                    @endif

                                    @if($product->is_featured)
                                        <span class="absolute top-3 right-3 bg-amber-400 text-white text-xs font-bold px-2.5 py-1 rounded-lg shadow-sm flex items-center gap-1">
                                            <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 20 20"><path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/></svg>
                                            Featured
                                        </span>
                                    @endif

                                    <div class="absolute inset-0 bg-black/0 group-hover:bg-black/5 transition-colors duration-300"></div>

                                    <div class="absolute top-3 right-3 flex flex-col gap-2 opacity-0 group-hover:opacity-100 translate-x-2 group-hover:translate-x-0 transition-all duration-300 {{ $product->is_featured ? 'top-14' : '' }}">
                                        <button title="Add to Wishlist" class="w-9 h-9 bg-white/90 backdrop-blur-sm rounded-xl flex items-center justify-center shadow-sm hover:bg-red-50 hover:text-red-500 transition-all text-gray-500">
                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"/></svg>
                                        </button>
                                        <button title="Quick View" class="w-9 h-9 bg-white/90 backdrop-blur-sm rounded-xl flex items-center justify-center shadow-sm hover:bg-emerald-50 hover:text-emerald-500 transition-all text-gray-500">
                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                                        </button>
                                    </div>
                                </div>

                                <div class="p-4">
                                    <a href="{{ route('products.show', $product->id) }}" class="block">
                                        <h3 class="text-sm font-semibold text-gray-800 hover:text-emerald-600 transition-colors line-clamp-2 min-h-[2.5rem]">{{ $product->name }}</h3>
                                    </a>

                                    @if($product->brand)
                                        <p class="text-xs text-gray-400 mt-1">{{ $product->brand->name }}</p>
                                    @endif

                                    <div class="flex items-baseline gap-2 mt-3">
                                        @if($hasSale)
                                            <span class="text-lg font-bold text-red-500">${{ number_format($product->sale_price, 2) }}</span>
                                            <span class="text-sm text-gray-400 line-through">${{ number_format($product->price, 2) }}</span>
                                        @else
                                            <span class="text-lg font-bold text-emerald-600">${{ number_format($product->price, 2) }}</span>
                                        @endif
                                    </div>

                                    <div class="flex items-center gap-1 mt-2">
                                        @for($i = 0; $i < 5; $i++)
                                            <svg class="w-3.5 h-3.5 {{ $i < 4 ? 'text-amber-400' : 'text-gray-200' }}" fill="currentColor" viewBox="0 0 20 20"><path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/></svg>
                                        @endfor
                                        <span class="text-xs text-gray-400 ml-1">(4.0)</span>
                                    </div>

                                    <div class="flex gap-2 mt-4">
                                        <a href="{{ route('products.show', $product->id) }}" class="flex-1 text-center text-xs font-medium py-2.5 rounded-xl border border-gray-200 text-gray-600 hover:border-emerald-300 hover:text-emerald-600 hover:bg-emerald-50 transition-all">
                                            <span class="hidden sm:inline">View Details</span>
                                            <svg class="sm:hidden w-4 h-4 mx-auto" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16V6a1 1 0 00-1-1H4a1 1 0 00-1 1v10a1 1 0 001 1h1m8-1a1 1 0 01-1 1H9m4-1V8a1 1 0 011-1h2.586a1 1 0 01.707.293l3.414 3.414a1 1 0 01.293.707V16a1 1 0 01-1 1h-1m-6-1a1 1 0 001 1h1M5 17a2 2 0 104 0m-4 0a2 2 0 114 0m6 0a2 2 0 104 0m-4 0a2 2 0 114 0"/></svg>
                                        </a>
                                        <form action="{{ route('cart.add', $product) }}" method="POST" class="flex-1">
                                            @csrf
                                            <input type="hidden" name="product_id" value="{{ $product->id }}">
                                            <input type="hidden" name="quantity" value="1">
                                            <button type="submit" class="w-full text-xs font-medium py-2.5 rounded-xl bg-emerald-600 text-white hover:bg-emerald-700 transition-all shadow-sm hover:shadow flex items-center justify-center gap-1 {{ $product->stock_quantity === 0 ? 'opacity-50 cursor-not-allowed bg-gray-400 hover:bg-gray-400' : '' }}">
                                                <svg class="w-4 h-4 sm:hidden" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 100 4 2 2 0 000-4z"/></svg>
                                                <span class="hidden sm:inline">{{ $product->stock_quantity === 0 ? 'Out of Stock' : 'Add to Cart' }}</span>
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>

                    @if($products->hasPages())
                        <div class="mt-10">
                            {{ $products->withQueryString()->links() }}
                        </div>
                    @endif

                @else
                    <div class="bg-white rounded-2xl shadow-sm border border-gray-100 p-16 text-center">
                        <div class="w-20 h-20 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-6">
                            <svg class="w-10 h-10 text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/></svg>
                        </div>
                        <h3 class="text-lg font-semibold text-gray-800 mb-2">No Products Found</h3>
                        <p class="text-sm text-gray-500 mb-6">There are no products in this category yet. Check back later!</p>
                        <a href="{{ route('home') }}" class="inline-flex items-center gap-2 px-6 py-3 bg-emerald-600 text-white text-sm font-medium rounded-xl hover:bg-emerald-700 transition-all shadow-sm hover:shadow">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/></svg>
                            Back to Home
                        </a>
                    </div>
                @endif
            </main>
        </div>
    </div>
</div>

@endsection
