@extends('layouts.app')

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

@section('content')
<div class="grid gap-8 xl:grid-cols-[1.2fr_0.8fr]">
    <div class="rounded-3xl bg-white p-8 shadow-sm">
        <img src="{{ $product->image }}" alt="{{ $product->name }}" class="w-full rounded-3xl object-cover h-[480px]">
        <div class="mt-8 space-y-4">
            <div class="flex flex-wrap items-center gap-3 text-sm text-slate-500">
                <span>{{ $product->category->name }}</span>
                <span class="h-1 w-1 rounded-full bg-slate-400"></span>
                <span>{{ number_format($product->rating, 1) }} ★ rating</span>
            </div>
            <h1 class="text-3xl font-semibold text-slate-900">{{ $product->name }}</h1>
            <p class="text-slate-600 leading-7">{{ $product->description }}</p>
            <div class="flex items-end gap-4">
                <div>
                    <p class="text-3xl font-bold text-slate-900">₹{{ number_format($product->display_price, 2) }}</p>
                    @if($product->sale_price)
                        <p class="text-sm text-slate-500 line-through">₹{{ number_format($product->price, 2) }}</p>
                    @endif
                </div>
                @if($product->discount_percent)
                    <span class="rounded-full bg-emerald-50 px-3 py-1 text-sm font-semibold text-emerald-700">{{ $product->discount_percent }}% OFF</span>
                @endif
            </div>
        </div>
    </div>

    <aside class="space-y-6">
        <div class="rounded-3xl bg-white p-6 shadow-sm">
            <h2 class="text-xl font-semibold mb-4">Purchase Options</h2>
            <form method="POST" action="{{ route('cart.add', $product->id) }}">
                @csrf
                <button type="submit" class="w-full rounded-full bg-slate-900 py-3 text-white font-semibold hover:bg-slate-800">Add to Cart</button>
            </form>
            <form method="POST" action="{{ route('wishlist.add', $product->id) }}" class="mt-3">
                @csrf
                <button type="submit" class="w-full rounded-full border border-slate-300 py-3 text-slate-700 font-semibold hover:bg-slate-50">Add to Wishlist</button>
            </form>
            <form method="POST" action="{{ route('compare.add', $product->id) }}" class="mt-3">
                @csrf
                <button type="submit" class="w-full rounded-full border border-slate-300 py-3 text-slate-700 font-semibold hover:bg-slate-50">Add to Compare</button>
            </form>
        </div>

        <div class="rounded-3xl bg-white p-6 shadow-sm">
            <h3 class="text-lg font-semibold mb-3">Delivery & Payment</h3>
            <ul class="space-y-3 text-slate-600 text-sm">
                <li>Free shipping over ₹1000</li>
                <li>30-day return policy</li>
                <li>Secure SSL payment</li>
            </ul>
        </div>
    </aside>
</div>

<!-- Reviews Section -->
<section class="mt-12 space-y-6">
    <h2 class="text-2xl font-semibold">Customer Reviews</h2>

    @auth
        <!-- Add Review -->
        <div class="rounded-3xl bg-white p-6 shadow-sm">
            <h3 class="text-lg font-semibold mb-4">Share Your Review</h3>
            <form action="{{ route('review.store', $product) }}" method="POST" class="space-y-4">
                @csrf
                
                <div>
                    <label class="block text-sm font-medium text-slate-700 mb-2">Rating</label>
                    <div class="flex gap-2">
                        @for($i = 1; $i <= 5; $i++)
                            <input type="radio" name="rating" value="{{ $i }}" id="star{{ $i }}" class="hidden" {{ $userReview && $userReview->rating == $i ? 'checked' : '' }}>
                            <label for="star{{ $i }}" class="cursor-pointer text-2xl text-slate-300 hover:text-yellow-400">★</label>
                        @endfor
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-slate-700 mb-2">Title</label>
                    <input type="text" name="title" required class="w-full rounded-3xl border border-slate-200 px-4 py-2 focus:border-slate-900 focus:outline-none" value="{{ $userReview?->title ?? '' }}">
                </div>

                <div>
                    <label class="block text-sm font-medium text-slate-700 mb-2">Comment</label>
                    <textarea name="comment" rows="4" required class="w-full rounded-3xl border border-slate-200 px-4 py-2 focus:border-slate-900 focus:outline-none">{{ $userReview?->comment ?? '' }}</textarea>
                </div>

                <button type="submit" class="rounded-full bg-slate-900 px-6 py-2 text-white hover:bg-slate-800">
                    {{ $userReview ? 'Update Review' : 'Submit Review' }}
                </button>
            </form>
        </div>
    @else
        <div class="rounded-3xl bg-slate-50 p-6 border border-slate-200">
            <p class="text-slate-600">
                <a href="{{ route('login') }}" class="font-semibold text-slate-900 hover:underline">Log in</a> to share your review
            </p>
        </div>
    @endauth

    <!-- Reviews List -->
    @if($reviews->isNotEmpty())
        <div class="space-y-4">
            @foreach($reviews as $review)
                <div class="rounded-3xl bg-white p-6 shadow-sm">
                    <div class="flex justify-between items-start mb-3">
                        <div>
                            <p class="font-semibold">{{ $review->user->name }}</p>
                            <div class="flex gap-1 text-yellow-400">
                                @for($i = 0; $i < $review->rating; $i++)
                                    <span>★</span>
                                @endfor
                            </div>
                        </div>
                        <p class="text-sm text-slate-500">{{ $review->created_at->diffForHumans() }}</p>
                    </div>
                    <h4 class="font-semibold mb-2">{{ $review->title }}</h4>
                    <p class="text-slate-600">{{ $review->comment }}</p>
                    @auth
                        @if(auth()->id() === $review->user_id || auth()->user()->isAdmin())
                            <form action="{{ route('review.delete', $review) }}" method="POST" class="mt-3 inline">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="text-sm text-red-600 hover:text-red-800" onclick="return confirm('Are you sure?')">Delete</button>
                            </form>
                        @endif
                    @endauth
                </div>
            @endforeach
        </div>

        {{ $reviews->links() }}
    @else
        <p class="text-slate-600">No reviews yet. Be the first to review this product!</p>
    @endif
</section>

@if($related->isNotEmpty())
<section class="mt-12">
    <div class="flex items-center justify-between mb-6">
        <div>
            <h2 class="text-2xl font-semibold">Related Products</h2>
            <p class="text-slate-500">Other items from the same category.</p>
        </div>
    </div>
    <div class="grid gap-6 md:grid-cols-2 xl:grid-cols-4">
        @foreach($related as $item)
            <article class="rounded-3xl bg-white p-5 shadow-sm hover:shadow-md transition">
                <img src="{{ $item->image }}" alt="{{ $item->name }}" class="h-40 w-full rounded-3xl object-cover">
                <div class="mt-4">
                    <h3 class="text-lg font-semibold"><a href="{{ route('product.show', $item->slug) }}">{{ $item->name }}</a></h3>
                    <div class="mt-2 flex items-center justify-between">
                        <p class="font-bold">₹{{ number_format($item->display_price, 2) }}</p>
                        <span class="text-sm text-slate-500">{{ number_format($item->rating, 1) }} ★</span>
                    </div>
                </div>
            </article>
        @endforeach
    </div>
</section>
@endif
@endsection
