<x-app-layout>
    <x-module-hero
        :title="$session->title"
        :description="($session->assignment->mentor->name ?? 'Mentor') . ' & ' . ($session->assignment->mentee->name ?? 'Mentee')"
        icon="fa-calendar-check">

        <x-slot name="backButton">
            <a href="{{ route('mentorship.sessions.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 Sessions
            </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 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>
            @endcan
            @cannot('view mentorship applications')
                @can('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> My Applications
                    </a>
                @endcan
            @endcannot
            @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 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 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('download mentorship certificates')
                @cannot('view 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>
                @endcannot
            @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 own mentorship payments')
                @cannot('view 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>
                @endcannot
            @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($session->status === 'scheduled')
                @can('edit mentorship sessions')
                    <a href="{{ route('mentorship.sessions.edit', $session) }}"
                       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-edit"></i> Edit
                    </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">
            {{-- 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-info-circle text-green-600"></i> Session Information
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
                        <div>
                            <p class="text-xs text-gray-500">Type</p>
                            <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium {{ $session->type === 'online' ? 'bg-blue-100 text-blue-800' : 'bg-purple-100 text-purple-800' }}">
                                <i class="fas {{ $session->type === 'online' ? 'fa-video' : 'fa-building' }}"></i>
                                {{ $session->type === 'online' ? 'Online Meeting' : 'In Person' }}
                            </span>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Status</p>
                            @php $statusColors = ['scheduled' => 'bg-blue-100 text-blue-800', 'completed' => 'bg-green-100 text-green-800', 'cancelled' => 'bg-red-100 text-red-800', 'no_show' => 'bg-yellow-100 text-yellow-800']; @endphp
                            <span class="px-2 py-0.5 rounded-full text-xs font-semibold {{ $statusColors[$session->status] ?? 'bg-gray-100 text-gray-800' }}">
                                {{ ucfirst(str_replace('_', ' ', $session->status)) }}
                            </span>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Date</p>
                            <p class="font-medium text-gray-900">{{ $session->start_time?->format('l, M d, Y') ?? 'N/A' }}</p>
                        </div>
                        <div>
                            <p class="text-xs text-gray-500">Time</p>
                            <p class="font-medium text-gray-900">
                                {{ $session->start_time?->format('h:i A') ?? 'N/A' }}
                                @if($session->end_time) – {{ $session->end_time->format('h:i A') }} @endif
                            </p>
                        </div>
                    </div>
                    @if($session->description)
                        <div class="mt-4 pt-4 border-t border-gray-100">
                            <p class="text-xs text-gray-500 mb-1">Description</p>
                            <p class="text-sm text-gray-700">{{ $session->description }}</p>
                        </div>
                    @endif
                </div>

                <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-map-marker-alt text-green-600"></i> Location
                    </h3>
                    @if($session->type === 'online' && $session->meeting_link)
                        <div class="flex items-center gap-4">
                            <div class="p-3 bg-blue-100 rounded-lg">
                                <i class="fas fa-video text-blue-500 text-xl"></i>
                            </div>
                            <div class="flex-1">
                                <p class="font-medium text-gray-900 text-sm">Online Meeting</p>
                                <a href="{{ $session->meeting_link }}" target="_blank"
                                   class="text-blue-600 hover:text-blue-800 text-xs break-all">{{ $session->meeting_link }}</a>
                            </div>
                        </div>
                        @if($session->status === 'scheduled')
                            <div class="mt-3">
                                <a href="{{ $session->meeting_link }}" target="_blank"
                                   class="inline-flex items-center gap-2 px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition text-sm">
                                    <i class="fas fa-external-link-alt"></i> Join Meeting
                                </a>
                            </div>
                        @endif
                    @elseif($session->venue)
                        <div class="flex items-center gap-4">
                            <div class="p-3 bg-purple-100 rounded-lg">
                                <i class="fas fa-building text-purple-500 text-xl"></i>
                            </div>
                            <div>
                                <p class="font-medium text-gray-900 text-sm">In Person</p>
                                <p class="text-sm text-gray-600">{{ $session->venue }}</p>
                            </div>
                        </div>
                    @else
                        <p class="text-sm text-gray-500">Location not specified</p>
                    @endif
                </div>

                @if($session->status === 'completed' && $session->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> Session Notes
                        </h3>
                        <p class="text-sm text-gray-700 whitespace-pre-line">{{ $session->notes }}</p>
                    </div>
                @endif

                @if($session->status === 'completed' && $session->summary)
                    <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-file-alt text-green-600"></i> Session Summary
                        </h3>
                        <p class="text-sm text-gray-700 whitespace-pre-line">{{ $session->summary }}</p>
                    </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-3 flex items-center gap-2">
                        <i class="fas fa-link text-green-600"></i> Assignment
                    </h3>
                    <a href="{{ route('mentorship.assignments.show', $session->assignment) }}"
                       class="block p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition">
                        <p class="font-medium text-gray-900 text-sm">{{ $session->assignment->programme->title ?? 'N/A' }}</p>
                        <p class="text-xs text-gray-500 mt-1">{{ $session->assignment->mentor->name }} & {{ $session->assignment->mentee->name }}</p>
                    </a>
                </div>

                <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-users text-green-600"></i> Participants
                    </h3>
                    <div class="space-y-3">
                        <div class="flex items-center gap-3">
                            <div class="w-9 h-9 bg-purple-100 rounded-full flex items-center justify-center">
                                <i class="fas fa-user-tie text-purple-600 text-sm"></i>
                            </div>
                            <div>
                                <p class="font-medium text-gray-900 text-sm">{{ $session->assignment->mentor->name }}</p>
                                <p class="text-xs text-gray-500">Mentor</p>
                            </div>
                        </div>
                        <div class="flex items-center gap-3">
                            <div class="w-9 h-9 bg-blue-100 rounded-full flex items-center justify-center">
                                <i class="fas fa-user text-blue-600 text-sm"></i>
                            </div>
                            <div>
                                <p class="font-medium text-gray-900 text-sm">{{ $session->assignment->mentee->name }}</p>
                                <p class="text-xs text-gray-500">Mentee</p>
                            </div>
                        </div>
                    </div>
                </div>

                @if($session->status === 'completed')
                    <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-check text-green-600"></i> Attendance
                        </h3>
                        <div class="space-y-2">
                            <div class="flex items-center justify-between p-2 bg-gray-50 rounded text-sm">
                                <span class="text-gray-700">{{ $session->assignment->mentor->name }}</span>
                                @if($session->mentor_attended)
                                    <span class="text-green-600 text-xs"><i class="fas fa-check-circle mr-1"></i>Present</span>
                                @else
                                    <span class="text-red-600 text-xs"><i class="fas fa-times-circle mr-1"></i>Absent</span>
                                @endif
                            </div>
                            <div class="flex items-center justify-between p-2 bg-gray-50 rounded text-sm">
                                <span class="text-gray-700">{{ $session->assignment->mentee->name }}</span>
                                @if($session->mentee_attended)
                                    <span class="text-green-600 text-xs"><i class="fas fa-check-circle mr-1"></i>Present</span>
                                @else
                                    <span class="text-red-600 text-xs"><i class="fas fa-times-circle mr-1"></i>Absent</span>
                                @endif
                            </div>
                        </div>
                    </div>
                @endif

                @if($session->status === 'scheduled')
                    <div class="bg-white rounded-lg border border-gray-200 p-6">
                        <h3 class="text-base font-semibold text-gray-800 mb-3">Quick Actions</h3>
                        <div class="space-y-2">
                            @can('complete mentorship sessions')
                                <form action="{{ route('mentorship.sessions.complete', $session) }}" method="POST"
                                      onsubmit="return confirm('Mark this session as complete?')">
                                    @csrf
                                    <button type="submit"
                                            class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-green-100 text-green-800 rounded-lg hover:bg-green-200 transition text-sm">
                                        <i class="fas fa-check-circle"></i> Mark Complete
                                    </button>
                                </form>
                            @endcan
                            @can('cancel mentorship sessions')
                                <form action="{{ route('mentorship.sessions.cancel', $session) }}" method="POST"
                                      onsubmit="return confirm('Cancel this session?')">
                                    @csrf
                                    <button type="submit"
                                            class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-red-100 text-red-800 rounded-lg hover:bg-red-200 transition text-sm">
                                        <i class="fas fa-times-circle"></i> Cancel Session
                                    </button>
                                </form>
                            @endcan
                        </div>
                    </div>
                @endif
            </div>
        </div>
    </div>
</x-app-layout>
