<x-app-layout>
    <x-module-hero
        title="Payment Status"
        :description="'Reference: ' . $payment->reference"
        icon="fa-credit-card">

        <x-slot name="tabs">
            @can('view mentorship payments')
                <a href="{{ route('mentorship.payments.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.payments.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                    <i class="fas fa-credit-card text-sm"></i> All Payments
                </a>
            @endcan
            @can('view own mentorship payments')
                <a href="{{ route('mentorship.payments.my-payments') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.payments.my-payments') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                    <i class="fas fa-history text-sm"></i> My Payments
                </a>
            @endcan
            @can('view mentorship programmes')
                <a href="{{ route('mentorship.programmes.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.programmes.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                    <i class="fas fa-list text-sm"></i> Programmes
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        <div class="max-w-5xl mx-auto">
            {{-- Status Card --}}
            <div class="bg-white rounded-lg border border-gray-200 overflow-hidden mb-4">
                <div class="p-8 text-center
                    @if($payment->isSuccessful()) bg-green-500
                    @elseif($payment->isFailed()) bg-red-500
                    @else bg-yellow-500 @endif">
                    @if($payment->isSuccessful())
                        <div class="w-20 h-20 mx-auto mb-4 bg-white/20 rounded-full flex items-center justify-center">
                            <i class="fas fa-check text-white text-4xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-white mb-1">Payment Successful!</h2>
                        <p class="text-white/90 text-sm">Your payment has been processed successfully.</p>
                    @elseif($payment->isFailed())
                        <div class="w-20 h-20 mx-auto mb-4 bg-white/20 rounded-full flex items-center justify-center">
                            <i class="fas fa-times text-white text-4xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-white mb-1">Payment Failed</h2>
                        <p class="text-white/90 text-sm">{{ $payment->error_message ?? 'An error occurred during payment processing.' }}</p>
                    @else
                        <div class="w-20 h-20 mx-auto mb-4 bg-white/20 rounded-full flex items-center justify-center">
                            <i class="fas fa-spinner fa-spin text-white text-4xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-white mb-1">Processing Payment...</h2>
                        <p class="text-white/90 text-sm">Please wait while we confirm your payment.</p>
                    @endif
                </div>

                <div class="p-6">
                    <dl class="space-y-3 text-sm">
                        <div class="flex justify-between py-2 border-b border-gray-100">
                            <dt class="text-gray-500">Reference</dt>
                            <dd class="font-mono font-semibold text-gray-900">{{ $payment->reference }}</dd>
                        </div>
                        <div class="flex justify-between py-2 border-b border-gray-100">
                            <dt class="text-gray-500">Amount</dt>
                            <dd class="text-lg font-bold text-green-600">{{ $payment->formatted_amount }}</dd>
                        </div>
                        <div class="flex justify-between py-2 border-b border-gray-100">
                            <dt class="text-gray-500">Payment Method</dt>
                            <dd class="font-medium text-gray-900">
                                @if(in_array($payment->payment_method, ['mtn_momo', 'mtn_momo_mobile']))
                                    <i class="fas fa-mobile-alt text-yellow-500 mr-1"></i> MTN Mobile Money
                                @elseif($payment->payment_method === 'mtn_momo_card')
                                    <i class="fas fa-credit-card text-indigo-500 mr-1"></i> Bank Card (MTN MoMo)
                                @else
                                    {{ ucfirst(str_replace('_', ' ', $payment->payment_method ?? 'N/A')) }}
                                @endif
                            </dd>
                        </div>
                        <div class="flex justify-between py-2">
                            <dt class="text-gray-500">Date</dt>
                            <dd class="font-medium text-gray-900">{{ $payment->created_at->format('M d, Y H:i') }}</dd>
                        </div>
                    </dl>

                    @if($payment->isPending() || $payment->status === 'processing')
                        <div class="mt-4 p-3 bg-blue-50 border border-blue-200 rounded-lg text-center text-sm text-blue-700">
                            <i class="fas fa-sync-alt fa-spin mr-1"></i>
                            Checking payment status automatically every 5 seconds…
                        </div>
                    @endif
                </div>
            </div>

            {{-- Next Steps --}}
            <div class="bg-white rounded-lg border border-gray-200 p-6">
                <h3 class="text-base font-semibold text-gray-800 mb-4 flex items-center gap-2">
                    <i class="fas fa-info-circle text-green-600"></i> What's Next?
                </h3>

                @if($payment->isSuccessful())
                    <div class="space-y-3 mb-4">
                        <div class="flex items-start gap-3">
                            <div class="w-6 h-6 rounded-full bg-green-500 flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-check text-white text-xs"></i>
                            </div>
                            <div>
                                <p class="text-sm font-medium text-gray-900">Payment Confirmed</p>
                                <p class="text-xs text-gray-500">Your application is now being processed.</p>
                            </div>
                        </div>
                        <div class="flex items-start gap-3">
                            <div class="w-6 h-6 rounded-full bg-blue-100 flex items-center justify-center flex-shrink-0">
                                <span class="text-blue-600 text-xs font-bold">2</span>
                            </div>
                            <div>
                                <p class="text-sm font-medium text-gray-900">Application Review</p>
                                <p class="text-xs text-gray-500">Our team will review your application within 2-3 business days.</p>
                            </div>
                        </div>
                        <div class="flex items-start gap-3">
                            <div class="w-6 h-6 rounded-full bg-blue-100 flex items-center justify-center flex-shrink-0">
                                <span class="text-blue-600 text-xs font-bold">3</span>
                            </div>
                            <div>
                                <p class="text-sm font-medium text-gray-900">Notification</p>
                                <p class="text-xs text-gray-500">You'll receive an email once your application is approved.</p>
                            </div>
                        </div>
                    </div>
                    <div class="flex flex-col sm:flex-row gap-3">
                        <a href="{{ route('mentorship.payments.receipt', $payment) }}" target="_blank"
                           class="flex-1 text-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm">
                            <i class="fas fa-download mr-1"></i> Download Receipt
                        </a>
                        @if($payment->application)
                            <a href="{{ route('mentorship.public.status', $payment->application) }}"
                               class="flex-1 text-center px-4 py-2 border border-gray-300 rounded-lg hover:bg-gray-50 transition text-sm">
                                <i class="fas fa-eye mr-1"></i> View Application
                            </a>
                        @endif
                    </div>
                @elseif($payment->isFailed())
                    <div class="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-700">
                        <p class="font-medium mb-1">Common reasons for payment failure:</p>
                        <ul class="list-disc list-inside space-y-0.5 text-xs">
                            <li>Insufficient balance on your mobile money account</li>
                            <li>Incorrect PIN entered</li>
                            <li>Network connectivity issues</li>
                        </ul>
                    </div>
                    <div class="flex flex-col sm:flex-row gap-3">
                        @if($payment->canBeRetried())
                            <form action="{{ route('mentorship.payments.retry', $payment) }}" method="POST" class="flex-1">
                                @csrf
                                <button type="submit"
                                        class="w-full px-4 py-2 bg-yellow-500 text-white rounded-lg hover:bg-yellow-600 transition text-sm">
                                    <i class="fas fa-redo mr-1"></i> Try Again
                                </button>
                            </form>
                        @endif
                        <a href="{{ route('mentorship.payments.index') }}"
                           class="flex-1 text-center px-4 py-2 border border-gray-300 rounded-lg hover:bg-gray-50 transition text-sm">
                            <i class="fas fa-list mr-1"></i> View All Payments
                        </a>
                    </div>
                @else
                    <div class="mb-4 p-3 bg-yellow-50 border border-yellow-200 rounded-lg text-sm text-yellow-700">
                        <i class="fas fa-lightbulb text-yellow-500 mr-1"></i>
                        Please don't close this page. If you've completed the payment on your phone, the confirmation should appear shortly.
                    </div>
                    <a href="{{ route('mentorship.payments.show', $payment) }}"
                       class="block text-center px-4 py-2 border border-gray-300 rounded-lg hover:bg-gray-50 transition text-sm">
                        <i class="fas fa-eye mr-1"></i> View Payment Details
                    </a>
                @endif
            </div>

            <div class="mt-4 text-center text-xs text-gray-500">
                <i class="fas fa-question-circle mr-1"></i>
                Having issues? <a href="mailto:support@example.com" class="text-green-600 hover:underline">Contact Support</a>
            </div>
        </div>
    </div>

    @if($payment->isPending() || $payment->status === 'processing')
        @push('scripts')
        <script>
            (function () {
                const paymentId  = {{ $payment->id }};
                const checkUrl   = '{{ route('mentorship.payments.check', $payment) }}';
                let attempts     = 0;
                const maxAttempts = 24; // 2 minutes

                const interval = setInterval(async function () {
                    attempts++;
                    if (attempts > maxAttempts) {
                        clearInterval(interval);
                        return;
                    }
                    try {
                        const res  = await fetch(checkUrl, {
                            headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }
                        });
                        const data = await res.json();
                        if (data.status && !['pending', 'processing'].includes(data.status)) {
                            clearInterval(interval);
                            window.location.reload();
                        }
                    } catch (e) { /* keep polling */ }
                }, 5000);
            })();
        </script>
        @endpush
    @endif
</x-app-layout>
