@extends('layouts.app')

@section('title', 'Certificate Details - ' . $certificate->certificate_uuid)
@section('page_header', 'Certificate Details & Actions')

@section('content')
<div class="max-w-5xl mx-auto space-y-6" x-data="{ revokeModalOpen: false }">

    <!-- Top Action & Info Bar -->
    <div class="flex flex-col md:flex-row items-start md:items-center justify-between gap-4 bg-white p-6 rounded-3xl border border-slate-200/80 shadow-sm">
        <div class="space-y-1">
            <div class="flex items-center gap-3">
                <span class="font-mono text-base font-extrabold text-emerald-700 bg-emerald-50 px-3 py-1 rounded-xl border border-emerald-200">
                    {{ $certificate->certificate_uuid }}
                </span>
                @if($certificate->status === 'active')
                    <span class="text-xs font-bold text-emerald-700 bg-emerald-100 px-3 py-1 rounded-full">🟢 Active & Verified</span>
                @else
                    <span class="text-xs font-bold text-rose-700 bg-rose-100 px-3 py-1 rounded-full">🔴 Revoked</span>
                @endif
            </div>
            <p class="text-xs text-slate-500">Issued on {{ date('d F Y', strtotime($certificate->issue_date)) }} by {{ $certificate->issuer->name ?? 'Admin' }}</p>
        </div>

        <div class="flex items-center gap-3 flex-wrap">
            <!-- Download PDF Button -->
            <a href="{{ route('admin.certificates.download_pdf', $certificate->id) }}" 
               class="inline-flex items-center gap-2 bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs px-5 py-3 rounded-xl shadow-lg shadow-emerald-600/20 transition-all hover:scale-105">
                <i data-lucide="download" class="w-4 h-4"></i>
                <span>Download / Print PDF</span>
            </a>

            <!-- Resend Email Button -->
            <form action="{{ route('admin.certificates.resend_email', $certificate->id) }}" method="POST">
                @csrf
                <button type="submit" class="inline-flex items-center gap-2 bg-slate-100 hover:bg-slate-200 text-slate-700 font-bold text-xs px-4 py-3 rounded-xl border border-slate-200 transition-all">
                    <i data-lucide="send" class="w-4 h-4 text-emerald-600"></i>
                    <span>Resend Email</span>
                </button>
            </form>

            <!-- Revoke Button (Super Admin or Authorized) -->
            @if($certificate->status === 'active')
            <button @click="revokeModalOpen = true" 
                    class="inline-flex items-center gap-2 bg-rose-50 hover:bg-rose-100 text-rose-700 font-bold text-xs px-4 py-3 rounded-xl border border-rose-200 transition-all">
                <i data-lucide="alert-octagon" class="w-4 h-4"></i>
                <span>Revoke Certificate</span>
            </button>
            @endif
        </div>
    </div>

    <!-- Main Grid: Info + Status Tracking -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
        
        <!-- Left: Metadata Cards (2 cols) -->
        <div class="md:col-span-2 space-y-6">
            
            <div class="bg-white p-8 rounded-3xl border border-slate-200/80 shadow-sm space-y-6">
                <h3 class="text-sm font-extrabold text-slate-900 uppercase tracking-wider border-b border-slate-100 pb-3">Certificate Information</h3>

                <div class="grid grid-cols-1 sm:grid-cols-2 gap-6 text-sm">
                    <div>
                        <span class="block text-xs font-semibold text-slate-400 uppercase">Student Name</span>
                        <p class="font-extrabold text-slate-900 text-lg">
                            <a href="{{ route('admin.students.show', $certificate->student->id) }}" class="hover:text-emerald-600">
                                {{ $certificate->student->full_name }}
                            </a>
                        </p>
                    </div>

                    <div>
                        <span class="block text-xs font-semibold text-slate-400 uppercase">College Institution</span>
                        <p class="font-bold text-slate-800">{{ $certificate->student->college->name ?? 'Direct Applicant' }}</p>
                    </div>

                    <div>
                        <span class="block text-xs font-semibold text-slate-400 uppercase">Internship Domain</span>
                        <p class="font-bold text-emerald-700">{{ $certificate->course->title ?? 'N/A' }}</p>
                    </div>

                    <div>
                        <span class="block text-xs font-semibold text-slate-400 uppercase">Internship Mode & Duration</span>
                        <p class="font-semibold text-slate-800">{{ $certificate->duration }} • {{ $certificate->internship_mode }}</p>
                    </div>

                    <div>
                        <span class="block text-xs font-semibold text-slate-400 uppercase">Internship Period</span>
                        <p class="font-semibold text-slate-800">
                            {{ date('d M Y', strtotime($certificate->start_date)) }} — {{ date('d M Y', strtotime($certificate->end_date)) }}
                        </p>
                    </div>

                    <div>
                        <span class="block text-xs font-semibold text-slate-400 uppercase">Designation / Role</span>
                        <p class="font-semibold text-slate-800">{{ $certificate->role_designation }}</p>
                    </div>
                </div>
            </div>

            <!-- Online Verification URL Card -->
            <div class="bg-white p-6 rounded-3xl border border-slate-200/80 shadow-sm space-y-3">
                <h4 class="text-xs font-bold text-slate-400 uppercase tracking-wider">Public Verification URL & QR Link</h4>
                <div class="flex items-center gap-3 bg-slate-50 p-3 rounded-2xl border border-slate-200">
                    <input type="text" readonly value="{{ $certificate->verification_url }}" 
                           class="flex-1 bg-transparent text-xs font-mono text-slate-700 outline-none select-all">
                    <a href="{{ $certificate->verification_url }}" target="_blank" 
                       class="px-3 py-1.5 bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs rounded-xl shadow">
                        Test QR Scan URL ↗
                    </a>
                </div>
            </div>

        </div>

        <!-- Right: Status Tracking Badges (1 col) -->
        <div class="space-y-6">
            
            <div class="bg-white p-6 rounded-3xl border border-slate-200/80 shadow-sm space-y-6">
                <h3 class="text-xs font-extrabold text-slate-400 uppercase tracking-wider border-b border-slate-100 pb-3">Delivery & Print Tracking</h3>

                <!-- Email Status -->
                <div class="space-y-2">
                    <span class="text-xs font-bold text-slate-700">Email Delivery Status</span>
                    @if($certificate->email_sent)
                        <div class="p-3 rounded-2xl bg-emerald-50 border border-emerald-200 text-emerald-800 text-xs font-semibold space-y-1">
                            <div class="flex items-center gap-2">
                                <i data-lucide="check-circle" class="w-4 h-4 text-emerald-600"></i>
                                <span>Sent Successfully</span>
                            </div>
                            <p class="text-[11px] text-emerald-600 font-normal">Dispatched on {{ $certificate->email_sent_at ? $certificate->email_sent_at->format('d M Y, h:i A') : 'N/A' }}</p>
                        </div>
                    @else
                        <div class="p-3 rounded-2xl bg-slate-100 border border-slate-200 text-slate-600 text-xs font-semibold">
                            ⚪ Not Sent via Email Yet
                        </div>
                    @endif
                </div>

                <!-- Print Count Status -->
                <div class="space-y-2">
                    <span class="text-xs font-bold text-slate-700">Print / Download Counter</span>
                    <div class="p-3 rounded-2xl bg-slate-50 border border-slate-200 flex items-center justify-between text-xs font-semibold">
                        <span class="text-slate-600">Total Downloads</span>
                        <span class="font-extrabold text-slate-900 bg-white px-3 py-1 rounded-xl border border-slate-200">
                            🖨 {{ $certificate->print_count }} Times
                        </span>
                    </div>
                </div>

                <!-- Signatory Title -->
                <div class="space-y-1 pt-3 border-t border-slate-100">
                    <span class="text-[11px] font-semibold text-slate-400">Authorized Signatory</span>
                    <p class="text-xs font-bold text-slate-800">{{ $certificate->signatory_title }}</p>
                </div>
            </div>

        </div>
    </div>

    <!-- REVOCATION MODAL -->
    <div x-show="revokeModalOpen" x-cloak class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-900/60 backdrop-blur-sm">
        <div class="bg-white rounded-3xl p-8 max-w-md w-full space-y-6 shadow-2xl">
            <div class="flex items-center justify-between border-b border-slate-100 pb-4">
                <div class="flex items-center gap-2 text-rose-700">
                    <i data-lucide="alert-triangle" class="w-6 h-6"></i>
                    <h3 class="text-lg font-bold text-slate-900">Revoke Certificate</h3>
                </div>
                <button @click="revokeModalOpen = false" class="text-slate-400 hover:text-slate-600">
                    <i data-lucide="x" class="w-5 h-5"></i>
                </button>
            </div>

            <form action="{{ route('admin.certificates.revoke', $certificate->id) }}" method="POST" class="space-y-4">
                @csrf
                <p class="text-xs text-slate-500 leading-relaxed">
                    Revoking certificate <span class="font-mono font-bold text-slate-800">{{ $certificate->certificate_uuid }}</span> will display a warning status on the public QR verification page.
                </p>

                <div>
                    <label class="block text-xs font-bold text-slate-700 uppercase tracking-wider mb-2">Revocation Reason <span class="text-rose-500">*</span></label>
                    <textarea name="revocation_reason" required rows="3" placeholder="Enter official reason for revoking this certificate..."
                              class="w-full px-4 py-3 rounded-xl border border-slate-200 bg-slate-50 text-sm font-semibold focus:bg-white focus:border-rose-600 outline-none"></textarea>
                </div>

                <div class="flex items-center justify-end gap-3 pt-4 border-t border-slate-100">
                    <button type="button" @click="revokeModalOpen = false" class="px-4 py-2.5 rounded-xl border border-slate-200 text-xs font-bold text-slate-600 hover:bg-slate-50">
                        Cancel
                    </button>
                    <button type="submit" class="px-5 py-2.5 rounded-xl bg-rose-600 hover:bg-rose-700 text-white text-xs font-bold shadow-lg shadow-rose-600/20">
                        Confirm Revocation
                    </button>
                </div>
            </form>
        </div>
    </div>

</div>
@endsection
