<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('My Watchlist') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    @if($watchlist->count() > 0)
                        <div class="mb-6">
                            <p class="text-gray-600">{{ $watchlist->total() }} items in your watchlist</p>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                            @foreach($watchlist as $watch)
                                <div class="group border rounded-lg overflow-hidden hover:shadow-lg transition-shadow">
                                    <a href="{{ route('items.show', $watch->item) }}" class="block">
                                        <div class="aspect-square bg-gray-200 overflow-hidden relative">
                                            @if($watch->item->primaryPhoto)
                                                <img src="{{ asset($watch->item->primaryPhoto->url) }}" alt="{{ $watch->item->title }}" class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-200">
                                            @else
                                                <div class="w-full h-full flex items-center justify-center">
                                                    <span class="text-gray-400">No Image</span>
                                                </div>
                                            @endif
                                            
                                            <!-- Status Badge -->
                                            @if($watch->item->status === 'sale_pending')
                                                <div class="absolute top-2 left-2 bg-orange-500 text-white text-xs px-2 py-1 rounded">
                                                    Sale Pending
                                                </div>
                                            @endif
                                            
                                            <!-- Listing Type Badge -->
                                            @if($watch->item->listing_type === 'trade')
                                                <div class="absolute top-2 right-2 bg-green-500 text-white text-xs px-2 py-1 rounded">
                                                    Trade
                                                </div>
                                            @endif

                                            <!-- Watched Date -->
                                            <div class="absolute bottom-2 left-2 bg-black bg-opacity-75 text-white text-xs px-2 py-1 rounded">
                                                Added {{ $watch->created_at->diffForHumans() }}
                                            </div>
                                        </div>
                                        
                                        <div class="p-4">
                                            <h3 class="font-semibold text-gray-900 group-hover:text-blue-600 line-clamp-2 mb-1">{{ $watch->item->title }}</h3>
                                            @if($watch->item->category)
                                                <p class="text-sm text-gray-600 mb-1">{{ $watch->item->category->name }}</p>
                                            @endif
                                            
                                            @if($watch->item->price && $watch->item->listing_type === 'sale')
                                                <p class="font-bold text-lg text-gray-900">${{ number_format($watch->item->price, 2) }}</p>
                                            @else
                                                <p class="font-bold text-lg text-green-600">Trade Only</p>
                                            @endif
                                            
                                            <div class="flex items-center justify-between text-xs text-gray-500 mt-2">
                                                <span>{{ $watch->item->user->name }}</span>
                                                <span>{{ $watch->item->created_at->diffForHumans() }}</span>
                                            </div>
                                            
                                            @if($watch->item->location_city || $watch->item->location_state)
                                                <p class="text-xs text-gray-500 mt-1">
                                                    {{ collect([$watch->item->location_city, $watch->item->location_state])->filter()->join(', ') }}
                                                </p>
                                            @endif
                                        </div>
                                    </a>
                                    
                                    <!-- Remove from watchlist button -->
                                    <div class="px-4 pb-4">
                                        <form method="POST" action="{{ route('items.watchlist.remove', $watch->item) }}" class="w-full">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="w-full inline-flex items-center justify-center px-3 py-2 bg-red-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-red-700 transition ease-in-out duration-150" onclick="return confirm('Remove this item from your watchlist?')">
                                                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                                                </svg>
                                                Remove
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            @endforeach
                        </div>

                        <!-- Pagination -->
                        <div class="mt-8">
                            {{ $watchlist->links() }}
                        </div>
                    @else
                        <div class="text-center py-12">
                            <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"></path>
                            </svg>
                            <h3 class="text-lg font-semibold text-gray-900 mb-2 mt-4">Your watchlist is empty</h3>
                            <p class="text-gray-600 mb-6">Start adding items you're interested in to keep track of them here.</p>
                            <a href="{{ route('items.index') }}" class="inline-flex items-center px-4 py-2 bg-blue-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-blue-700 transition ease-in-out duration-150">
                                Browse Items
                            </a>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>