@extends('layouts.app')

@section('title', $product->name)

@section('content')
<div class="bg-gray-50 min-h-screen py-8">
    <div class="container mx-auto px-4">
        <!-- Breadcrumb -->
        <nav class="mb-6">
            <ol class="flex items-center space-x-2 text-sm">
                <li><a href="{{ route('home') }}" class="text-gray-500 hover:text-blue-600">Home</a></li>
                <li class="text-gray-400">/</li>
                <li><a href="{{ route('products.index') }}" class="text-gray-500 hover:text-blue-600">Products</a></li>
                @if($product->category)
                    <li class="text-gray-400">/</li>
                    <li><a href="{{ route('products.index', ['category' => $product->category->slug]) }}" class="text-gray-500 hover:text-blue-600">{{ $product->category->name }}</a></li>
                @endif
                <li class="text-gray-400">/</li>
                <li class="text-gray-900 font-medium">{{ Str::limit($product->name, 30) }}</li>
            </ol>
        </nav>

        <!-- Product Details -->
        <div class="bg-white rounded-2xl shadow-sm overflow-hidden">
            <div class="grid md:grid-cols-2 gap-8 p-8">
                <!-- Product Image -->
                <div class="relative">
                    <div class="aspect-square bg-gray-100 rounded-xl overflow-hidden">
                        @if($product->image_url)
                            <img src="{{ $product->image_url }}" alt="{{ $product->name }}" class="w-full h-full object-cover">
                        @else
                            <div class="w-full h-full flex items-center justify-center">
                                <span class="text-9xl text-gray-300">📦</span>
                            </div>
                        @endif
                    </div>
                    
                    @if($product->is_featured)
                        <span class="absolute top-4 left-4 bg-yellow-400 text-yellow-900 text-xs font-bold px-3 py-1 rounded-full">
                            Featured
                        </span>
                    @endif
                    
                    @if($product->compare_price && $product->compare_price > $product->price)
                        @php $discount = round((($product->compare_price - $product->price) / $product->compare_price) * 100); @endphp
                        <span class="absolute top-4 right-4 bg-red-500 text-white text-xs font-bold px-3 py-1 rounded-full">
                            -{{ $discount }}%
                        </span>
                    @endif
                </div>

                <!-- Product Info -->
                <div>
                    <div class="mb-4">
                        @if($product->supplier)
                            <a href="#" class="text-sm text-blue-600 hover:underline">{{ $product->supplier->company_name }}</a>
                        @endif
                    </div>

                    <h1 class="text-3xl font-bold text-gray-900 mb-4">{{ $product->name }}</h1>

                    <!-- Rating -->
                    <div class="flex items-center gap-2 mb-4">
                        <div class="flex text-yellow-400">
                            @for($i = 1; $i <= 5; $i++)
                                @if($i <= $product->rating)
                                    <span>★</span>
                                @else
                                    <span class="text-gray-300">★</span>
                                @endif
                            @endfor
                        </div>
                        <span class="text-sm text-gray-600">({{ $product->rating }}/5)</span>
                    </div>

                    <!-- Price -->
                    <div class="mb-6">
                        <div class="flex items-baseline gap-3">
                            <span class="text-4xl font-bold text-blue-600">R {{ number_format($product->price, 2) }}</span>
                            @if($product->compare_price && $product->compare_price > $product->price)
                                <span class="text-xl text-gray-400 line-through">R {{ number_format($product->compare_price, 2) }}</span>
                            @endif
                        </div>
                        <p class="text-sm text-gray-500 mt-1">Price per {{ $product->unit }}</p>
                        <p class="text-sm text-gray-500">VAT included</p>
                    </div>

                    <!-- Stock Status -->
                    <div class="mb-6">
                        @if($product->stock_quantity > 10)
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-800">
                                <span class="w-2 h-2 bg-green-500 rounded-full mr-2"></span>
                                In Stock ({{ $product->stock_quantity }} available)
                            </span>
                        @elseif($product->stock_quantity > 0)
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-800">
                                <span class="w-2 h-2 bg-yellow-500 rounded-full mr-2"></span>
                                Low Stock ({{ $product->stock_quantity }} left)
                            </span>
                        @else
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-800">
                                <span class="w-2 h-2 bg-red-500 rounded-full mr-2"></span>
                                Out of Stock
                            </span>
                        @endif
                    </div>

                    <!-- Add to Cart -->
                    @auth
                        @if(auth()->user()->user_type === 'company' && $product->stock_quantity > 0)
                            <form action="{{ route('cart.add') }}" method="POST" class="mb-6">
                                @csrf
                                <input type="hidden" name="product_id" value="{{ $product->id }}">
                                
                                <div class="flex items-center gap-4 mb-4">
                                    <label class="text-sm font-medium text-gray-700">Quantity:</label>
                                    <div class="flex items-center border border-gray-300 rounded-lg">
                                        <button type="button" onclick="decrementQty()" class="px-3 py-2 text-gray-600 hover:bg-gray-100">-</button>
                                        <input type="number" name="quantity" id="quantity" value="1" min="{{ $product->min_order_quantity }}" 
                                            class="w-16 text-center border-0 focus:ring-0">
                                        <button type="button" onclick="incrementQty()" class="px-3 py-2 text-gray-600 hover:bg-gray-100">+</button>
                                    </div>
                                    <span class="text-sm text-gray-500">Min: {{ $product->min_order_quantity }}</span>
                                </div>

                                <button type="submit" class="w-full bg-blue-600 text-white py-3 px-6 rounded-xl font-semibold hover:bg-blue-700 transition flex items-center justify-center gap-2">
                                    <span>🛒</span> Add to Cart
                                </button>
                            </form>
                        @elseif($product->stock_quantity <= 0)
                            <button disabled class="w-full bg-gray-300 text-gray-500 py-3 px-6 rounded-xl font-semibold cursor-not-allowed mb-6">
                                Out of Stock
                            </button>
                        @endif
                    @else
                        <a href="{{ route('login') }}" class="block w-full bg-blue-600 text-white py-3 px-6 rounded-xl font-semibold hover:bg-blue-700 transition text-center mb-6">
                            Login to Purchase
                        </a>
                    @endauth

                    <!-- Product Details -->
                    <div class="border-t pt-6">
                        <h3 class="font-semibold text-gray-900 mb-3">Product Details</h3>
                        <dl class="grid grid-cols-2 gap-4 text-sm">
                            <div>
                                <dt class="text-gray-500">SKU</dt>
                                <dd class="font-medium">{{ $product->sku }}</dd>
                            </div>
                            <div>
                                <dt class="text-gray-500">Category</dt>
                                <dd class="font-medium">{{ $product->category?->name ?? 'Uncategorized' }}</dd>
                            </div>
                            <div>
                                <dt class="text-gray-500">Unit</dt>
                                <dd class="font-medium">{{ $product->unit }}</dd>
                            </div>
                            <div>
                                <dt class="text-gray-500">Min Order</dt>
                                <dd class="font-medium">{{ $product->min_order_quantity }} {{ $product->unit }}</dd>
                            </div>
                        </dl>
                    </div>
                </div>
            </div>

            <!-- Description -->
            <div class="border-t px-8 py-6">
                <h3 class="font-semibold text-gray-900 mb-3">Description</h3>
                <div class="prose max-w-none text-gray-600">
                    {!! nl2br(e($product->description)) !!}
                </div>
            </div>
        </div>

        <!-- Price Comparison -->
        @if($priceComparison->count() > 0)
            <div class="mt-8 bg-white rounded-2xl shadow-sm p-8">
                <h2 class="text-xl font-bold text-gray-900 mb-6">Compare Prices from Other Suppliers</h2>
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead>
                            <tr class="border-b">
                                <th class="text-left py-3 px-4">Product</th>
                                <th class="text-left py-3 px-4">Supplier</th>
                                <th class="text-right py-3 px-4">Price</th>
                                <th class="text-center py-3 px-4">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($priceComparison as $item)
                                <tr class="border-b hover:bg-gray-50">
                                    <td class="py-3 px-4">{{ $item->name }}</td>
                                    <td class="py-3 px-4 text-gray-600">{{ $item->supplier?->company_name }}</td>
                                    <td class="py-3 px-4 text-right font-semibold">R {{ number_format($item->price, 2) }}</td>
                                    <td class="py-3 px-4 text-center">
                                        <a href="{{ route('products.show', $item->id) }}" class="text-blue-600 hover:underline">View</a>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        @endif

        <!-- Similar Products -->
        @if($similarProducts->count() > 0)
            <div class="mt-8">
                <h2 class="text-xl font-bold text-gray-900 mb-6">Similar Products</h2>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-6">
                    @foreach($similarProducts as $similar)
                        @include('components.product-card', ['product' => $similar])
                    @endforeach
                </div>
            </div>
        @endif
    </div>
</div>

<script>
function incrementQty() {
    const input = document.getElementById('quantity');
    input.value = parseInt(input.value) + 1;
}

function decrementQty() {
    const input = document.getElementById('quantity');
    const min = parseInt(input.min) || 1;
    if (parseInt(input.value) > min) {
        input.value = parseInt(input.value) - 1;
    }
}
</script>
@endsection
