<x-app-layout>
    <x-module-hero
        :title="'Certificate: ' . $certificate->certificate_number"
        :description="$certificate->recipient_name . ' — ' . $certificate->programme_name"
        icon="fa-certificate">

        <x-slot name="backButton">
            <a href="{{ route('mentorship.certificates.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 Certificates
            </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
            @can('view mentorship certificates')
                <a href="{{ route('mentorship.certificates.index') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.certificates.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-certificate text-sm"></i> All Certificates
                </a>
            @endcan
            @can('download mentorship certificates')
                <a href="{{ route('mentorship.certificates.my-certificates') }}"
                   class="module-hero-tab {{ request()->routeIs('mentorship.certificates.my-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-award text-sm"></i> My Certificates
                </a>
            @endcan
            @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 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">
            @if($certificate->status === 'active')
                @can('download mentorship certificates')
                    <a href="{{ route('mentorship.certificates.download', $certificate) }}"
                       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-download"></i> Download PDF
                    </a>
                @endcan
            @endif
        </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">
            <div class="lg:col-span-2">
                <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-amber-500"></i> Certificate Information
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
                        <div>
                            <p class="text-xs text-gray-500">Certificate Number</p>
                            <p class="font-mono font-semibold text-gray-900">{{ $certificate->certificate_number }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Verification Code</p>
                            <p class="font-mono font-semibold text-gray-900">{{ $certificate->verification_code }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Recipient Name</p>
                            <p class="font-semibold text-gray-900">{{ $certificate->recipient_name }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Programme</p>
                            <p class="font-semibold text-gray-900">{{ $certificate->programme_name }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Issue Date</p>
                            <p class="font-semibold text-gray-900">{{ $certificate->issue_date?->format('F d, Y') ?? 'N/A' }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Completion Date</p>
                            <p class="font-semibold text-gray-900">{{ $certificate->completion_date?->format('F d, Y') ?? 'N/A' }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Status</p>
                            <span class="px-2 py-0.5 rounded-full text-xs font-semibold
                                @if($certificate->status === 'active') bg-green-100 text-green-800
                                @else bg-red-100 text-red-800 @endif">
                                {{ ucfirst($certificate->status) }}
                            </span>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Generated By</p>
                            <p class="font-semibold text-gray-900">{{ $certificate->generator?->name ?? 'System' }}</p>
                        </div>
                    </div>

                    @if($certificate->skills_acquired && count($certificate->skills_acquired) > 0)
                        <div class="mt-4 pt-4 border-t border-gray-100">
                            <p class="text-xs text-gray-500 mb-2">Skills Acquired</p>
                            <div class="flex flex-wrap gap-2">
                                @foreach($certificate->skills_acquired as $skill)
                                    <span class="px-2 py-0.5 rounded-full text-xs font-medium bg-amber-100 text-amber-800">{{ $skill }}</span>
                                @endforeach
                            </div>
                        </div>
                    @endif
                </div>
            </div>

            <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">Actions</h3>
                    <div class="space-y-2">
                        @if($certificate->status === 'active')
                            @can('download mentorship certificates')
                                <a href="{{ route('mentorship.certificates.download', $certificate) }}"
                                   class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-amber-500 text-white rounded-lg hover:bg-amber-600 transition text-sm">
                                    <i class="fas fa-download"></i> Download PDF
                                </a>
                            @endcan
                        @endif
                        <a href="{{ route('mentorship.certificates.verify-page') }}"
                           class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-blue-100 text-blue-700 rounded-lg hover:bg-blue-200 transition text-sm">
                            <i class="fas fa-shield-alt"></i> Verify Online
                        </a>
                        @can('generate mentorship certificates')
                            <form method="POST" action="{{ route('mentorship.certificates.regenerate', $certificate) }}">
                                @csrf
                                <button type="submit"
                                        class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition text-sm">
                                    <i class="fas fa-sync-alt"></i> Regenerate PDF
                                </button>
                            </form>
                        @endcan
                        @can('revoke mentorship certificates')
                            @if($certificate->status === 'active')
                                <button onclick="document.getElementById('revokeModal').classList.remove('hidden')"
                                        class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-red-100 text-red-700 rounded-lg hover:bg-red-200 transition text-sm">
                                    <i class="fas fa-ban"></i> Revoke Certificate
                                </button>
                            @endif
                        @endcan
                    </div>
                </div>

                @if($certificate->assignment && $certificate->assignment->mentor)
                    <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-user-tie text-purple-500"></i> Mentor
                        </h3>
                        <p class="font-semibold text-gray-900 text-sm">{{ $certificate->assignment->mentor->name }}</p>
                        <p class="text-xs text-gray-500">{{ $certificate->assignment->mentor->email }}</p>
                    </div>
                @endif
            </div>
        </div>
    </div>

    {{-- Revoke Modal --}}
    @can('revoke mentorship certificates')
        @if($certificate->status === 'active')
            <div id="revokeModal" 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-ban"></i> Revoke Certificate
                        </h3>
                        <button type="button" onclick="document.getElementById('revokeModal').classList.add('hidden')"
                                class="text-white/80 hover:text-white transition"><i class="fas fa-times"></i></button>
                    </div>
                    <div class="p-6">
                    <form method="POST" action="{{ route('mentorship.certificates.revoke', $certificate) }}">
                        @csrf
                        <div class="mb-4">
                            <label for="reason" class="block text-sm font-medium text-gray-700 mb-1">
                                Reason for Revocation <span class="text-red-500">*</span>
                            </label>
                            <textarea name="reason" id="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..."></textarea>
                        </div>
                        <div class="flex justify-end gap-2">
                            <button type="button" onclick="document.getElementById('revokeModal').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-ban mr-1"></i> Revoke
                            </button>
                        </div>
                    </form>
                    </div>
                </div>
            </div>
        @endif
    @endcan
</x-app-layout>
