<x-app-layout>
    <x-module-hero
        title="Application Details"
        :description="$application->name . ' — ' . ($application->programme->title ?? 'N/A')"
        icon="fa-file-alt">

        <x-slot name="backButton">
            <a href="{{ route('mentorship.applications.index') }}"
               class="inline-flex items-center gap-1 text-sm text-[#475569] hover:text-[#1e293b] transition-colors">
                <i class="fas fa-arrow-left text-xs"></i> Back to Applications
            </a>
        </x-slot>

        <x-slot name="tabs">
            @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
            @canany(['view mentorship applications', 'view own mentorship applications'])
                <a href="{{ route('mentorship.applications.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.applications.*') ? '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-file-alt text-sm"></i> Applications
                </a>
            @endcanany
            @can('view mentorship assignments')
                <a href="{{ route('mentorship.assignments.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.assignments.*') ? '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-link text-sm"></i> Assignments
                </a>
            @endcan
            @can('view mentorship sessions')
                <a href="{{ route('mentorship.sessions.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.sessions.index') || request()->routeIs('mentorship.sessions.show') || request()->routeIs('mentorship.sessions.create') || request()->routeIs('mentorship.sessions.edit') ? '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-calendar-alt text-sm"></i> Sessions
                </a>
                <a href="{{ route('mentorship.sessions.calendar') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.sessions.calendar') ? '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-calendar text-sm"></i> Sessions Calendar
                </a>
            @endcan
            @can('view mentorship certificates')
                <a href="{{ route('mentorship.certificates.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.certificates.*') ? '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-certificate text-sm"></i> Certificates
                </a>
            @endcan
            @can('view mentorship payments')
                <a href="{{ route('mentorship.payments.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.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-credit-card text-sm"></i> Payments
                </a>
            @endcan
            @can('view mentorship categories')
                <a href="{{ route('mentorship.categories.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.categories.*') ? '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-folder text-sm"></i> Categories
                </a>
            @endcan
        </x-slot>

        <x-slot name="actions">
            @can('review mentorship applications')
                <a href="{{ route('mentorship.applications.review', $application) }}"
                   class="inline-flex items-center gap-2 px-4 py-2 bg-white text-green-800 text-sm font-medium rounded hover:bg-green-50 transition-colors">
                    <i class="fas fa-clipboard-check"></i> Full Review
                </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        @if(session('success'))
            <div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-2">
                <i class="fas fa-check-circle"></i> {{ session('success') }}
            </div>
        @endif

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            {{-- Main Content --}}
            <div class="lg:col-span-2 space-y-4">
                <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-user text-green-600"></i> Applicant Information
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
                        <div>
                            <label class="text-xs text-gray-500 block">Full Name</label>
                            <p class="font-medium text-gray-900">{{ $application->name }}</p>
                        </div>
                        <div>
                            <label class="text-xs text-gray-500 block">Email</label>
                            <p class="font-medium text-gray-900">{{ $application->email }}</p>
                        </div>
                        <div>
                            <label class="text-xs text-gray-500 block">Phone</label>
                            <p class="font-medium text-gray-900">{{ $application->phone ?? 'N/A' }}</p>
                        </div>
                        <div>
                            <label class="text-xs text-gray-500 block">Role Preference</label>
                            <span class="px-2 py-0.5 rounded-full text-xs font-semibold
                                @if($application->role_preference === 'mentor') bg-purple-100 text-purple-800
                                @else bg-blue-100 text-blue-800 @endif">
                                {{ ucfirst($application->role_preference) }}
                            </span>
                        </div>
                        <div>
                            <label class="text-xs text-gray-500 block">Programme</label>
                            <p class="font-medium text-gray-900">{{ $application->programme->title ?? 'N/A' }}</p>
                        </div>
                        <div>
                            <label class="text-xs text-gray-500 block">Category</label>
                            <p class="font-medium text-gray-900">{{ $application->category->name ?? 'N/A' }}</p>
                        </div>
                    </div>
                </div>

                @if($application->experience || $application->motivation || $application->expectations)
                    <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-briefcase text-green-600"></i> Experience & Motivation
                        </h3>
                        <div class="space-y-4 text-sm">
                            @if($application->experience)
                                <div>
                                    <label class="text-xs text-gray-500 block mb-1">Relevant Experience</label>
                                    <p class="text-gray-900 bg-gray-50 p-3 rounded-lg">{!! nl2br(e($application->experience)) !!}</p>
                                </div>
                            @endif
                            @if($application->motivation)
                                <div>
                                    <label class="text-xs text-gray-500 block mb-1">Motivation</label>
                                    <p class="text-gray-900 bg-gray-50 p-3 rounded-lg">{!! nl2br(e($application->motivation)) !!}</p>
                                </div>
                            @endif
                            @if($application->expectations)
                                <div>
                                    <label class="text-xs text-gray-500 block mb-1">Expectations</label>
                                    <p class="text-gray-900 bg-gray-50 p-3 rounded-lg">{!! nl2br(e($application->expectations)) !!}</p>
                                </div>
                            @endif
                        </div>
                    </div>
                @endif

                @if($application->review_notes)
                    <div class="bg-white rounded-lg border border-gray-200 p-6">
                        <h3 class="text-base font-semibold text-gray-800 mb-3 flex items-center gap-2">
                            <i class="fas fa-sticky-note text-green-600"></i> Review Notes
                        </h3>
                        <p class="text-sm text-gray-900 bg-gray-50 p-3 rounded-lg">{!! nl2br(e($application->review_notes)) !!}</p>
                    </div>
                @endif

                @if($application->user)
                    <div class="bg-white rounded-lg border border-gray-200 p-6">
                        <h3 class="text-base font-semibold text-gray-800 mb-3 flex items-center gap-2">
                            <i class="fas fa-link text-green-600"></i> Linked User Account
                        </h3>
                        <div class="flex items-center gap-4">
                            <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                                <i class="fas fa-user text-green-600"></i>
                            </div>
                            <div>
                                <p class="font-medium text-gray-900 text-sm">{{ $application->user->name }}</p>
                                <p class="text-xs text-gray-500">{{ $application->user->email }}</p>
                            </div>
                        </div>
                    </div>
                @endif
            </div>

            {{-- Sidebar --}}
            <div class="space-y-4">
                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <h3 class="text-base font-semibold text-gray-800 mb-4">Application Status</h3>
                    <div class="text-center mb-4">
                        <span class="px-4 py-2 rounded-full text-sm font-semibold
                            @if($application->status === 'approved') bg-green-100 text-green-800
                            @elseif($application->status === 'rejected') bg-red-100 text-red-800
                            @elseif($application->status === 'pending') bg-yellow-100 text-yellow-800
                            @else bg-gray-100 text-gray-800 @endif">
                            {{ ucfirst(str_replace('_', ' ', $application->status)) }}
                        </span>
                    </div>
                    <div class="space-y-2 text-sm">
                        <div class="flex justify-between">
                            <span class="text-gray-500">Applied</span>
                            <span class="font-medium">{{ $application->created_at->format('M d, Y') }}</span>
                        </div>
                        @if($application->reviewed_at)
                            <div class="flex justify-between">
                                <span class="text-gray-500">Reviewed</span>
                                <span class="font-medium">{{ $application->reviewed_at->format('M d, Y') }}</span>
                            </div>
                        @endif
                        @if($application->reviewer)
                            <div class="flex justify-between">
                                <span class="text-gray-500">Reviewer</span>
                                <span class="font-medium">{{ $application->reviewer->name }}</span>
                            </div>
                        @endif
                    </div>
                </div>

                @if($application->payment_required)
                    <div class="bg-white rounded-lg border border-gray-200 p-6">
                        <h3 class="text-base font-semibold text-gray-800 mb-4">Payment Status</h3>
                        <div class="text-center mb-3">
                            <span class="px-4 py-2 rounded-full text-sm font-semibold
                                @if($application->payment_status === 'paid') bg-green-100 text-green-800
                                @elseif($application->payment_status === 'pending') bg-yellow-100 text-yellow-800
                                @else bg-red-100 text-red-800 @endif">
                                {{ ucfirst($application->payment_status ?? 'unpaid') }}
                            </span>
                        </div>
                        <div class="text-center">
                            <p class="text-lg font-bold text-gray-900">
                                {{ number_format($application->programme->fee_amount ?? 0, 2) }} {{ $application->programme->currency ?? '' }}
                            </p>
                        </div>
                    </div>
                @endif

                @canany(['approve mentorship applications', 'reject mentorship applications', 'review mentorship applications'])
                    <div class="bg-white rounded-lg border border-gray-200 p-6">
                        <h3 class="text-base font-semibold text-gray-800 mb-4">Actions</h3>
                        <div class="space-y-2">
                            @if($application->status === 'pending')
                                @can('review mentorship applications')
                                    <form action="{{ route('mentorship.applications.under-review', $application) }}" method="POST">
                                        @csrf
                                        <button type="submit"
                                                class="w-full px-4 py-2 bg-blue-100 text-blue-800 rounded-lg hover:bg-blue-200 transition text-left text-sm">
                                            <i class="fas fa-eye mr-2"></i> Mark Under Review
                                        </button>
                                    </form>
                                @endcan
                            @endif

                            @if(in_array($application->status, ['pending', 'under_review']))
                                @can('approve mentorship applications')
                                    <form action="{{ route('mentorship.applications.approve', $application) }}" method="POST">
                                        @csrf
                                        <button type="submit"
                                                class="w-full px-4 py-2 bg-green-100 text-green-800 rounded-lg hover:bg-green-200 transition text-left text-sm">
                                            <i class="fas fa-check mr-2"></i> Approve Application
                                        </button>
                                    </form>
                                @endcan
                                @can('reject mentorship applications')
                                    <button onclick="document.getElementById('rejectModal').classList.remove('hidden')"
                                            class="w-full px-4 py-2 bg-red-100 text-red-800 rounded-lg hover:bg-red-200 transition text-left text-sm">
                                        <i class="fas fa-times mr-2"></i> Reject Application
                                    </button>
                                @endcan
                            @endif
                        </div>
                    </div>
                @endcanany
            </div>
        </div>
    </div>

    {{-- Reject Modal --}}
    <div id="rejectModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center">
        <div class="bg-white rounded-lg w-full max-w-md mx-4 overflow-hidden">
            <div class="flex items-center justify-between px-6 py-4 bg-gradient-to-r from-red-600 to-red-700">
                <h3 class="text-base font-semibold text-white flex items-center gap-2">
                    <i class="fas fa-times-circle"></i> Reject Application
                </h3>
                <button type="button" onclick="document.getElementById('rejectModal').classList.add('hidden')"
                        class="text-white/80 hover:text-white transition"><i class="fas fa-times"></i></button>
            </div>
            <div class="p-6">
            <form action="{{ route('mentorship.applications.reject', $application) }}" method="POST">
                @csrf
                <div class="mb-4">
                    <label class="block text-sm font-medium text-gray-700 mb-1">Rejection Reason <span class="text-red-500">*</span></label>
                    <textarea name="rejection_reason" rows="3" required
                              class="w-full border-gray-300 rounded-lg focus:ring-red-500 focus:border-red-500 text-sm"
                              placeholder="Please provide a reason for rejection..."></textarea>
                </div>
                <div class="mb-4">
                    <label class="block text-sm font-medium text-gray-700 mb-1">Internal Notes</label>
                    <textarea name="review_notes" rows="2"
                              class="w-full border-gray-300 rounded-lg text-sm"></textarea>
                </div>
                <div class="flex justify-end gap-2">
                    <button type="button" onclick="document.getElementById('rejectModal').classList.add('hidden')"
                            class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 text-sm">Cancel</button>
                    <button type="submit" class="btn-action-delete">
                        <i class="fas fa-times mr-1"></i> Reject
                    </button>
                </div>
            </form>
            </div>
        </div>
    </div>
</x-app-layout>
