<x-app-layout>
    <x-module-hero
        title="{{ isset($isOwnOnly) && $isOwnOnly ? 'My Sessions' : 'Mentorship Sessions' }}"
        description="{{ isset($isOwnOnly) && $isOwnOnly ? 'View and manage your mentorship sessions' : 'Schedule and manage mentorship sessions' }}"
        icon="fa-calendar-alt">

        <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> {{ auth()->user()->can('view mentorship applications') ? 'Applications' : 'My 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> {{ (auth()->user()->hasRole('Mentor') || auth()->user()->hasRole('Mentee')) ? 'My Assignments' : '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> {{ isset($isOwnOnly) && $isOwnOnly ? 'My Sessions' : '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('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">
            @can('create mentorship sessions')
                <a href="{{ route('mentorship.sessions.create') }}"
                   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-plus"></i> Schedule Session
                </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
        @if(session('error'))
            <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
                <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
            </div>
        @endif

        {{-- Filters --}}
        <div class="bg-white rounded-lg border border-gray-200 p-4 mb-4">
            <form method="GET" action="{{ route('mentorship.sessions.index') }}" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-4">
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Status</label>
                    <select name="status" class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                        <option value="">All Statuses</option>
                        <option value="scheduled" {{ ($filters['status'] ?? '') == 'scheduled' ? 'selected' : '' }}>Scheduled</option>
                        <option value="completed" {{ ($filters['status'] ?? '') == 'completed' ? 'selected' : '' }}>Completed</option>
                        <option value="cancelled" {{ ($filters['status'] ?? '') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                        <option value="no_show" {{ ($filters['status'] ?? '') == 'no_show' ? 'selected' : '' }}>No Show</option>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Type</label>
                    <select name="type" class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                        <option value="">All Types</option>
                        <option value="online" {{ ($filters['type'] ?? '') == 'online' ? 'selected' : '' }}>Online</option>
                        <option value="in_person" {{ ($filters['type'] ?? '') == 'in_person' ? 'selected' : '' }}>In Person</option>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Date From</label>
                    <input type="date" name="start_date" value="{{ $filters['start_date'] ?? '' }}"
                           class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Date To</label>
                    <input type="date" name="end_date" value="{{ $filters['end_date'] ?? '' }}"
                           class="w-full border-gray-300 rounded-lg text-sm focus:ring-green-500 focus:border-green-500">
                </div>
                <div class="flex items-end gap-2">
                    <button type="submit" class="px-4 py-2 bg-green-600 text-white text-sm rounded-lg hover:bg-green-700 transition">
                        <i class="fas fa-search mr-1"></i> Filter
                    </button>
                    <a href="{{ route('mentorship.sessions.index') }}"
                       class="px-4 py-2 bg-gray-200 text-gray-700 text-sm rounded-lg hover:bg-gray-300 transition">
                        <i class="fas fa-times"></i>
                    </a>
                </div>
            </form>
        </div>

        {{-- Table --}}
        <div class="w-full overflow-x-auto">
            @if($sessions->count() > 0)
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="th-col-1 px-4 py-3 text-left">Title</th>
                            <th class="th-col-2 px-4 py-3 text-left">Assignment</th>
                            <th class="th-col-3 px-4 py-3 text-left">Type</th>
                            <th class="th-col-4 px-4 py-3 text-left">Date/Time</th>
                            <th class="th-col-5 px-4 py-3 text-left">Venue/Link</th>
                            <th class="th-col-6 px-4 py-3 text-left">Status</th>
                            <th class="th-col-7 px-4 py-3 text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($sessions as $session)
                            <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                                <td class="px-4 py-3">
                                    <div class="font-medium text-gray-900 text-sm">{{ $session->title }}</div>
                                    @if($session->description)
                                        <div class="text-xs text-gray-500">{{ Str::limit($session->description, 40) }}</div>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    <div class="text-gray-900">{{ $session->assignment->mentor->name ?? 'N/A' }} & {{ $session->assignment->mentee->name ?? 'N/A' }}</div>
                                    <div class="text-xs text-gray-500">{{ $session->assignment->programme->title ?? 'N/A' }}</div>
                                </td>
                                <td class="px-4 py-3">
                                    <span class="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' }} mr-1"></i>
                                        {{ $session->type === 'online' ? 'Online' : 'In Person' }}
                                    </span>
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    <div class="text-gray-900">{{ $session->start_time?->format('M d, Y') ?? 'N/A' }}</div>
                                    <div class="text-xs text-gray-500">
                                        {{ $session->start_time?->format('h:i A') ?? '' }}
                                        @if($session->end_time) – {{ $session->end_time->format('h:i A') }} @endif
                                    </div>
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    @if($session->type === 'online' && $session->meeting_link)
                                        <a href="{{ $session->meeting_link }}" target="_blank" class="text-blue-600 hover:text-blue-800 text-xs">
                                            <i class="fas fa-external-link-alt mr-1"></i>Join
                                        </a>
                                    @elseif($session->venue)
                                        <span class="text-xs text-gray-600">{{ Str::limit($session->venue, 30) }}</span>
                                    @else
                                        <span class="text-xs text-gray-400">Not specified</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3">
                                    @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>
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex justify-center gap-1">
                                        @can('view mentorship sessions')
                                            <a href="{{ route('mentorship.sessions.show', $session) }}" class="btn-action-view">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                        @endcan
                                        @if($session->status === 'scheduled')
                                            @can('edit mentorship sessions')
                                                <a href="{{ route('mentorship.sessions.edit', $session) }}" class="btn-action-edit">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            @endcan
                                            @can('complete mentorship sessions')
                                                <form action="{{ route('mentorship.sessions.complete', $session) }}" method="POST" class="inline"
                                                      onsubmit="return confirm('Mark this session as complete?')">
                                                    @csrf
                                                    <button type="submit" class="btn-action-approve">
                                                        <i class="fas fa-check-circle"></i>
                                                    </button>
                                                </form>
                                            @endcan
                                            @can('cancel mentorship sessions')
                                                <form action="{{ route('mentorship.sessions.cancel', $session) }}" method="POST" class="inline"
                                                      onsubmit="return confirm('Cancel this session?')">
                                                    @csrf
                                                    <button type="submit" class="btn-action-reject">
                                                        <i class="fas fa-times-circle"></i>
                                                    </button>
                                                </form>
                                            @endcan
                                        @endif
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                @if($sessions->hasPages())
                    <div class="px-4 py-3 border-t border-gray-200">
                        {{ $sessions->links() }}
                    </div>
                @endif
            @else
                <div class="text-center py-16">
                    <i class="fas fa-calendar-times text-gray-300 text-5xl mb-4"></i>
                    <h3 class="text-lg font-medium text-gray-900 mb-2">No Sessions Found</h3>
                    <p class="text-gray-500 mb-4">Schedule your first session to get started.</p>
                    @can('create mentorship sessions')
                        <a href="{{ route('mentorship.sessions.create') }}"
                           class="inline-flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition">
                            <i class="fas fa-plus"></i> Schedule Session
                        </a>
                    @endcan
                </div>
            @endif
        </div>
    </div>
</x-app-layout>
