<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
            {{ __('Commit to Buy') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-4xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    <!-- Item Summary -->
                    <div class="border-b border-gray-200 dark:border-gray-700 pb-6 mb-6">
                        <div class="flex items-start space-x-4">
                            <div class="w-24 h-24 bg-gray-200 dark:bg-gray-700 rounded-lg overflow-hidden flex-shrink-0">
                                @if($item->primaryPhoto)
                                    <img src="{{ asset($item->primaryPhoto->url) }}" alt="{{ $item->title }}" class="w-full h-full object-cover">
                                @else
                                    <div class="w-full h-full flex items-center justify-center">
                                        <span class="text-gray-400 dark:text-gray-500 text-xs">No Image</span>
                                    </div>
                                @endif
                            </div>
                            <div class="flex-1">
                                <h1 class="text-2xl font-bold text-gray-900 dark:text-gray-100">{{ $item->title }}</h1>
                                @if($item->category)
                                    <p class="text-gray-600 dark:text-gray-400">{{ $item->category->name }}</p>
                                @endif
                                <p class="text-2xl font-bold text-gray-900 dark:text-gray-100 mt-2">${{ number_format($item->price, 2) }}</p>
                                <p class="text-sm text-gray-500 dark:text-gray-400 mt-1">Listed by {{ $item->user->name }}</p>
                            </div>
                        </div>
                    </div>

                    @if($existingQueue)
                        <!-- Already in Queue -->
                        <div class="bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-lg p-6 mb-6">
                            <div class="flex items-start">
                                <svg class="w-6 h-6 text-gray-400 dark:text-gray-500 mt-1 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                </svg>
                                <div>
                                    <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-300 mb-2">You're Already in the Buy Queue</h3>
                                    @if($userPosition === 1)
                                        <p class="text-gray-700 dark:text-gray-300">You are the <strong>primary buyer</strong> for this item. The seller should contact you soon to arrange payment and delivery.</p>
                                    @else
                                        <p class="text-gray-700 dark:text-gray-300">You are backup buyer <strong>#{{ $userPosition - 1 }}</strong>. You'll be contacted if the primary sale falls through.</p>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="flex justify-center items-center space-x-6">
                            <a href="{{ route('items.show', $item) }}" class="inline-flex items-center px-4 py-2 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md font-semibold text-xs text-gray-700 dark:text-gray-300 uppercase tracking-widest hover:bg-gray-50 dark:hover:bg-gray-700 transition ease-in-out duration-150">
                                ← Back to Item
                            </a>
                            <form method="POST" action="{{ route('items.queue.leave', $item) }}" onsubmit="return confirm('Withdraw from the buy queue for this item?')">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="text-sm text-red-600 hover:text-red-800 font-medium underline">
                                    Withdraw from queue
                                </button>
                            </form>
                        </div>
                    @else
                        <!-- Buying Process Explanation -->
                        <div class="mb-8">
                            <h2 class="text-xl font-bold text-gray-900 dark:text-gray-100 mb-4">How the Buying Process Works</h2>
                            
                            @if($isPrimary)
                                <!-- Primary Buyer Flow -->
                                <div class="bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-lg p-6 mb-6">
                                    <div class="flex items-start">
                                        <svg class="w-6 h-6 text-gray-400 dark:text-gray-500 mt-1 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                        </svg>
                                        <div>
                                            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-300 mb-2">You'll be the Primary Buyer</h3>
                                            <p class="text-gray-700 dark:text-gray-300">Since no one else has committed to buying this item yet, you'll have first priority.</p>
                                        </div>
                                    </div>
                                </div>
                            @else
                                <!-- Backup Buyer Flow -->
                                <div class="bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-lg p-6 mb-6">
                                    <div class="flex items-start">
                                        <svg class="w-6 h-6 text-gray-400 dark:text-gray-500 mt-1 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z"></path>
                                        </svg>
                                        <div>
                                            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-300 mb-2">You'll be a Backup Buyer</h3>
                                            <p class="text-gray-700 dark:text-gray-300">{{ $buyQueue->count() }} {{ $buyQueue->count() === 1 ? 'person has' : 'people have' }} already committed to buying this item. You'll be backup buyer #{{ $buyQueue->count() }}.</p>
                                        </div>
                                    </div>
                                </div>
                            @endif

                            <!-- Process Steps -->
                            <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
                                <div class="text-center p-4 border border-gray-200 dark:border-gray-700 rounded-lg">
                                    <div class="w-12 h-12 bg-gray-100 dark:bg-gray-700 rounded-full flex items-center justify-center mx-auto mb-3">
                                        <span class="text-gray-700 dark:text-gray-300 font-bold text-lg">1</span>
                                    </div>
                                    <h4 class="font-semibold text-gray-900 dark:text-gray-100 mb-2">Commit to Buy</h4>
                                    <p class="text-sm text-gray-600 dark:text-gray-400">Click the button below to join the buy queue for this item.</p>
                                </div>

                                <div class="text-center p-4 border border-gray-200 dark:border-gray-700 rounded-lg">
                                    <div class="w-12 h-12 bg-gray-100 dark:bg-gray-700 rounded-full flex items-center justify-center mx-auto mb-3">
                                        <span class="text-gray-700 dark:text-gray-300 font-bold text-lg">2</span>
                                    </div>
                                    <h4 class="font-semibold text-gray-900 dark:text-gray-100 mb-2">Seller Contact</h4>
                                    <p class="text-sm text-gray-600 dark:text-gray-400">The seller will message you to arrange payment method and delivery.</p>
                                </div>

                                <div class="text-center p-4 border border-gray-200 dark:border-gray-700 rounded-lg">
                                    <div class="w-12 h-12 bg-gray-100 dark:bg-gray-700 rounded-full flex items-center justify-center mx-auto mb-3">
                                        <span class="text-gray-700 dark:text-gray-300 font-bold text-lg">3</span>
                                    </div>
                                    <h4 class="font-semibold text-gray-900 dark:text-gray-100 mb-2">Complete Sale</h4>
                                    <p class="text-sm text-gray-600 dark:text-gray-400">Meet up or arrange shipping, pay directly, and complete the transaction.</p>
                                </div>
                            </div>

                            <!-- Important Information -->
                            <div class="bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-lg p-6">
                                <h4 class="font-semibold text-gray-900 dark:text-gray-100 mb-3">Important Information:</h4>
                                <ul class="text-sm text-gray-600 dark:text-gray-400 space-y-2">
                                    <li class="flex items-start">
                                        <svg class="w-4 h-4 text-gray-400 dark:text-gray-500 mt-0.5 mr-2 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"></path>
                                        </svg>
                                        <span><strong>Person-to-person payments only:</strong> We don't process payments. You arrange payment directly with the seller.</span>
                                    </li>
                                    <li class="flex items-start">
                                        <svg class="w-4 h-4 text-gray-400 dark:text-gray-500 mt-0.5 mr-2 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"></path>
                                        </svg>
                                        <span><strong>Commitment matters:</strong> Only click "Buy" if you're serious about purchasing this item.</span>
                                    </li>
                                    <li class="flex items-start">
                                        <svg class="w-4 h-4 text-gray-400 dark:text-gray-500 mt-0.5 mr-2 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"></path>
                                        </svg>
                                        <span><strong>Queue system:</strong> First committed buyer gets priority. Backup buyers are contacted if the primary sale falls through.</span>
                                    </li>
                                    <li class="flex items-start">
                                        <svg class="w-4 h-4 text-gray-400 dark:text-gray-500 mt-0.5 mr-2 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"></path>
                                        </svg>
                                        <span><strong>Communication:</strong> All arrangements are made through our messaging system for your safety.</span>
                                    </li>
                                </ul>
                            </div>
                        </div>

                        <!-- Payment Methods -->
                        @if(count($item->user->payment_methods_display) > 0)
                            <div class="mb-8">
                                <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-4">Seller Accepts These Payment Methods</h3>
                                <div class="bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-lg p-4">
                                    <div class="grid grid-cols-2 gap-3">
                                        @foreach($item->user->payment_methods_display as $method)
                                            @php
                                                $methodNames = [
                                                    'paypal' => 'PayPal',
                                                    'venmo' => 'Venmo',
                                                    'cashapp' => 'Cash App',
                                                    'zelle' => 'Zelle',
                                                    'crypto_bitcoin' => 'Bitcoin',
                                                    'crypto_ethereum' => 'Ethereum',
                                                    'bank_transfer' => 'Bank Transfer',
                                                    'cash' => 'Cash (In Person)'
                                                ];
                                            @endphp
                                            <div class="flex items-center">
                                                <svg class="w-4 h-4 text-gray-400 dark:text-gray-500 mr-2" fill="currentColor" viewBox="0 0 20 20">
                                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path>
                                                </svg>
                                                <span class="text-sm font-medium text-gray-700 dark:text-gray-300">{{ $methodNames[$method] ?? ucwords(str_replace('_', ' ', $method)) }}</span>
                                            </div>
                                        @endforeach
                                    </div>
                                    <p class="text-xs text-gray-600 dark:text-gray-400 mt-3">
                                        <svg class="w-3 h-3 inline mr-1" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"></path>
                                        </svg>
                                        Payment details will be shared after you commit to purchase
                                    </p>
                                </div>
                            </div>
                        @else
                            <div class="mb-8">
                                <div class="bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-lg p-4">
                                    <div class="flex">
                                        <svg class="w-5 h-5 text-gray-400 dark:text-gray-500 mr-2" fill="currentColor" viewBox="0 0 20 20">
                                            <path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd"></path>
                                        </svg>
                                        <div class="text-sm text-gray-700 dark:text-gray-300">
                                            <p><strong>Payment methods not specified:</strong> The seller hasn't set up their payment preferences yet. Usual payment is via paypal, venmo, etc. You'll need to discuss payment methods directly with them.</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @endif

                        <!-- Action Buttons -->
                        <div class="flex items-center justify-between pt-6 border-t border-gray-200 dark:border-gray-700">
                            <a href="{{ route('items.show', $item) }}" class="inline-flex items-center px-4 py-2 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md font-semibold text-xs text-gray-700 dark:text-gray-300 uppercase tracking-widest hover:bg-gray-50 dark:hover:bg-gray-700 transition ease-in-out duration-150">
                                ← Back to Item
                            </a>

                            <form method="POST" action="{{ route('items.buy', $item) }}" class="inline">
                                @csrf
                                <button type="submit" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 transition ease-in-out duration-150" onclick="return confirm('Are you sure you want to commit to buying this item? This will add you to the buyer queue.')">
                                    @if($isPrimary)
                                        Yes, I'm Committed to Buying This
                                    @else
                                        Yes, Add Me as Backup Buyer #{{ $buyQueue->count() }}
                                    @endif
                                </button>
                            </form>
                        </div>
                    @endif
                </div>
            </div>

            @if(!$existingQueue && $buyQueue->count() > 0)
                <!-- Queue summary (names are only visible to the seller) -->
                <div class="mt-8 bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                    <div class="p-6 flex items-center space-x-3">
                        <div class="w-8 h-8 bg-gray-100 dark:bg-gray-700 rounded-full flex items-center justify-center">
                            <span class="text-gray-700 dark:text-gray-300 font-semibold text-sm">{{ $buyQueue->count() }}</span>
                        </div>
                        <p class="text-sm text-gray-700 dark:text-gray-300">
                            {{ $buyQueue->count() === 1 ? '1 person is' : $buyQueue->count() . ' people are' }} ahead of you in the queue.
                            The earliest committed buyer was {{ $buyQueue->first()->created_at->diffForHumans() }}.
                        </p>
                    </div>
                </div>
            @endif
        </div>
    </div>
</x-app-layout>