<x-app-layout>
    <x-module-hero
        :title="'Programme: ' . $programme->title"
        :description="($programme->category->name ?? 'Uncategorized') . ($programme->is_featured ? ' · Featured' : '')"
        icon="fa-chalkboard-teacher">

        <x-slot name="backButton">
            <a href="{{ route('mentorship.programmes.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 Programmes
            </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 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('edit mentorship programmes')
                <a href="{{ route('mentorship.programmes.edit', $programme) }}"
                   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
        </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

        {{-- Tab Navigation --}}
        <div class="flex flex-wrap gap-2 mb-6 border-b border-gray-200 pb-3">
            <button onclick="showTab('overview')" class="px-4 py-2 rounded-lg bg-green-600 text-white text-sm tab-btn" data-tab="overview">
                <i class="fas fa-info-circle mr-1"></i> Overview
            </button>
            <button onclick="showTab('applications')" class="px-4 py-2 rounded-lg bg-gray-100 text-gray-700 hover:bg-gray-200 text-sm tab-btn" data-tab="applications">
                <i class="fas fa-users mr-1"></i> Applications
            </button>
            <button onclick="showTab('assignments')" class="px-4 py-2 rounded-lg bg-gray-100 text-gray-700 hover:bg-gray-200 text-sm tab-btn" data-tab="assignments">
                <i class="fas fa-link mr-1"></i> Assignments
            </button>
            <button onclick="showTab('statistics')" class="px-4 py-2 rounded-lg bg-gray-100 text-gray-700 hover:bg-gray-200 text-sm tab-btn" data-tab="statistics">
                <i class="fas fa-chart-bar mr-1"></i> Statistics
            </button>
        </div>

        {{-- Overview Tab --}}
        <div id="overview-tab" class="tab-content">
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                <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-3 flex items-center gap-2">
                            <i class="fas fa-info-circle text-green-600"></i> Programme Details
                        </h3>
                        <div class="prose max-w-none text-gray-700 text-sm">{!! $programme->description !!}</div>
                    </div>
                    @if($programme->objectives)
                        <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-bullseye text-green-600"></i> Learning Objectives
                            </h3>
                            <div class="text-sm text-gray-700">{!! nl2br(e($programme->objectives)) !!}</div>
                        </div>
                    @endif
                    @if($programme->requirements)
                        <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-list-check text-green-600"></i> Requirements
                            </h3>
                            <div class="text-sm text-gray-700">{!! nl2br(e($programme->requirements)) !!}</div>
                        </div>
                    @endif
                </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-3 flex items-center gap-2">
                            <i class="fas fa-cog text-green-600"></i> Quick Info
                        </h3>
                        <dl class="space-y-2 text-sm">
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Status</dt>
                                <dd>
                                    @if($programme->is_active)
                                        <span class="px-2 py-0.5 bg-green-100 text-green-800 rounded-full text-xs font-semibold">Active</span>
                                    @else
                                        <span class="px-2 py-0.5 bg-red-100 text-red-800 rounded-full text-xs font-semibold">Inactive</span>
                                    @endif
                                </dd>
                            </div>
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Category</dt>
                                <dd class="font-medium">{{ $programme->category->name ?? 'Uncategorized' }}</dd>
                            </div>
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Fee</dt>
                                <dd class="font-medium">
                                    @if($programme->is_free)
                                        <span class="text-green-600">Free</span>
                                    @else
                                        {{ number_format($programme->fee_amount, 2) }} {{ $programme->currency }}
                                    @endif
                                </dd>
                            </div>
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Duration</dt>
                                <dd class="font-medium">{{ $programme->duration_text }}</dd>
                            </div>
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Sessions</dt>
                                <dd class="font-medium">{{ $programme->sessions_count ?? 'Flexible' }}</dd>
                            </div>
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Session Type</dt>
                                <dd class="font-medium capitalize">{{ str_replace('_', ' ', $programme->session_type) }}</dd>
                            </div>
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Certificate</dt>
                                <dd class="font-medium">
                                    @if($programme->certificate_enabled)
                                        <span class="text-green-600"><i class="fas fa-check"></i> Enabled</span>
                                    @else
                                        <span class="text-gray-400">Disabled</span>
                                    @endif
                                </dd>
                            </div>
                            <div class="flex justify-between">
                                <dt class="text-gray-500">Created</dt>
                                <dd class="font-medium">{{ $programme->created_at->format('M d, Y') }}</dd>
                            </div>
                        </dl>
                    </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-chart-pie text-green-600"></i> Quick Stats
                        </h3>
                        <div class="grid grid-cols-2 gap-3">
                            <div class="text-center p-3 bg-blue-50 rounded-lg">
                                <div class="text-xl font-bold text-blue-600">{{ $statistics['applications'] ?? 0 }}</div>
                                <div class="text-xs text-gray-500">Applications</div>
                            </div>
                            <div class="text-center p-3 bg-green-50 rounded-lg">
                                <div class="text-xl font-bold text-green-600">{{ $statistics['active_assignments'] ?? 0 }}</div>
                                <div class="text-xs text-gray-500">Active Pairs</div>
                            </div>
                            <div class="text-center p-3 bg-purple-50 rounded-lg">
                                <div class="text-xl font-bold text-purple-600">{{ $statistics['mentors'] ?? 0 }}</div>
                                <div class="text-xs text-gray-500">Mentors</div>
                            </div>
                            <div class="text-center p-3 bg-yellow-50 rounded-lg">
                                <div class="text-xl font-bold text-yellow-600">{{ $statistics['mentees'] ?? 0 }}</div>
                                <div class="text-xs text-gray-500">Mentees</div>
                            </div>
                        </div>
                    </div>

                    @can('edit mentorship programmes')
                        <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">
                                @if($programme->is_active)
                                    <button onclick="toggleStatus({{ $programme->id }}, false)"
                                            class="w-full px-4 py-2 bg-yellow-100 text-yellow-800 rounded-lg hover:bg-yellow-200 transition text-left text-sm">
                                        <i class="fas fa-pause mr-2"></i> Deactivate Programme
                                    </button>
                                @else
                                    <button onclick="toggleStatus({{ $programme->id }}, true)"
                                            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-play mr-2"></i> Activate Programme
                                    </button>
                                @endif
                                @can('create mentorship programmes')
                                    <form action="{{ route('mentorship.programmes.duplicate', $programme) }}" method="POST">
                                        @csrf
                                        <button type="submit"
                                                class="w-full px-4 py-2 bg-purple-100 text-purple-800 rounded-lg hover:bg-purple-200 transition text-left text-sm">
                                            <i class="fas fa-copy mr-2"></i> Duplicate Programme
                                        </button>
                                    </form>
                                @endcan
                            </div>
                        </div>
                    @endcan
                </div>
            </div>
        </div>

        {{-- Applications Tab --}}
        <div id="applications-tab" class="tab-content hidden">
            <div class="bg-white rounded-lg border border-gray-200 p-6">
                <div class="flex justify-between items-center mb-4">
                    <h3 class="text-base font-semibold text-gray-800">Recent Applications</h3>
                    @can('view mentorship applications')
                        <a href="{{ route('mentorship.applications.index', ['programme_id' => $programme->id]) }}"
                           class="text-green-600 hover:text-green-800 text-sm">
                            View All <i class="fas fa-arrow-right ml-1"></i>
                        </a>
                    @endcan
                </div>
                @if($recentApplications->count() > 0)
                    <div class="overflow-x-auto">
                        <table class="w-full table-zebra">
                            <thead>
                                <tr>
                                    <th class="th-col-1 px-4 py-3 text-left">Applicant</th>
                                    <th class="th-col-2 px-4 py-3 text-left">Role</th>
                                    <th class="th-col-3 px-4 py-3 text-left">Status</th>
                                    <th class="th-col-4 px-4 py-3 text-left">Applied</th>
                                    <th class="th-col-5 px-4 py-3 text-center">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($recentApplications as $application)
                                    <tr>
                                        <td class="px-4 py-3">
                                            <div class="font-medium text-sm">{{ $application->name }}</div>
                                            <div class="text-xs text-gray-500">{{ $application->email }}</div>
                                        </td>
                                        <td class="px-4 py-3 text-sm capitalize">{{ $application->role_preference }}</td>
                                        <td class="px-4 py-3">
                                            <span class="px-2 py-0.5 rounded-full text-xs font-semibold
                                                @if($application->status === 'approved') bg-green-100 text-green-800
                                                @elseif($application->status === 'rejected') bg-red-100 text-red-800
                                                @else bg-yellow-100 text-yellow-800 @endif">
                                                {{ ucfirst($application->status) }}
                                            </span>
                                        </td>
                                        <td class="px-4 py-3 text-xs text-gray-500">{{ $application->created_at->diffForHumans() }}</td>
                                        <td class="px-4 py-3 text-center">
                                            <a href="{{ route('mentorship.applications.show', $application) }}" class="btn-action-view">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="text-center py-8 text-gray-500 text-sm">
                        <i class="fas fa-inbox text-3xl mb-2 block"></i> No applications yet
                    </div>
                @endif
            </div>
        </div>

        {{-- Assignments Tab --}}
        <div id="assignments-tab" class="tab-content hidden">
            <div class="bg-white rounded-lg border border-gray-200 p-6">
                <div class="flex justify-between items-center mb-4">
                    <h3 class="text-base font-semibold text-gray-800">Active Assignments</h3>
                    @can('create mentorship assignments')
                        <a href="{{ route('mentorship.assignments.create', ['programme_id' => $programme->id]) }}"
                           class="btn-action-approve">
                            <i class="fas fa-plus"></i> Create Assignment
                        </a>
                    @endcan
                </div>
                @if($activeAssignments->count() > 0)
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                        @foreach($activeAssignments as $assignment)
                            <div class="border border-gray-200 rounded-lg p-4 hover:shadow-md transition">
                                <div class="flex items-center justify-between mb-3">
                                    <span class="px-2 py-0.5 rounded-full text-xs font-semibold
                                        @if($assignment->status === 'active') bg-green-100 text-green-800
                                        @else bg-yellow-100 text-yellow-800 @endif">
                                        {{ ucfirst($assignment->status) }}
                                    </span>
                                    <span class="text-xs text-gray-500">{{ $assignment->progress_percentage ?? 0 }}%</span>
                                </div>
                                <div class="space-y-1 text-sm">
                                    <div class="flex items-center gap-2">
                                        <i class="fas fa-user-tie text-green-600 w-4"></i>
                                        <span>{{ $assignment->mentor->name ?? 'Unassigned' }}</span>
                                    </div>
                                    <div class="flex items-center gap-2">
                                        <i class="fas fa-user text-blue-600 w-4"></i>
                                        <span>{{ $assignment->mentee->name ?? 'Unassigned' }}</span>
                                    </div>
                                </div>
                                <div class="mt-3 pt-3 border-t flex justify-end">
                                    <a href="{{ route('mentorship.assignments.show', $assignment) }}" class="btn-action-view">
                                        <i class="fas fa-eye"></i> View
                                    </a>
                                </div>
                            </div>
                        @endforeach
                    </div>
                @else
                    <div class="text-center py-8 text-gray-500 text-sm">
                        <i class="fas fa-link text-3xl mb-2 block"></i> No active assignments
                    </div>
                @endif
            </div>
        </div>

        {{-- Statistics Tab --}}
        <div id="statistics-tab" class="tab-content hidden">
            <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <div class="flex items-center gap-3">
                        <div class="p-3 bg-blue-100 rounded-lg"><i class="fas fa-file-alt text-blue-600 text-xl"></i></div>
                        <div>
                            <div class="text-xs text-gray-500">Total Applications</div>
                            <div class="text-2xl font-bold text-gray-900">{{ $statistics['applications'] ?? 0 }}</div>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <div class="flex items-center gap-3">
                        <div class="p-3 bg-green-100 rounded-lg"><i class="fas fa-check-circle text-green-600 text-xl"></i></div>
                        <div>
                            <div class="text-xs text-gray-500">Approved</div>
                            <div class="text-2xl font-bold text-gray-900">{{ $statistics['approved'] ?? 0 }}</div>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <div class="flex items-center gap-3">
                        <div class="p-3 bg-yellow-100 rounded-lg"><i class="fas fa-clock text-yellow-600 text-xl"></i></div>
                        <div>
                            <div class="text-xs text-gray-500">Pending</div>
                            <div class="text-2xl font-bold text-gray-900">{{ $statistics['pending'] ?? 0 }}</div>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <div class="flex items-center gap-3">
                        <div class="p-3 bg-purple-100 rounded-lg"><i class="fas fa-link text-purple-600 text-xl"></i></div>
                        <div>
                            <div class="text-xs text-gray-500">Active Pairs</div>
                            <div class="text-2xl font-bold text-gray-900">{{ $statistics['active_assignments'] ?? 0 }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        function showTab(tabName) {
            document.querySelectorAll('.tab-content').forEach(t => t.classList.add('hidden'));
            document.getElementById(tabName + '-tab').classList.remove('hidden');
            document.querySelectorAll('.tab-btn').forEach(btn => {
                if (btn.dataset.tab === tabName) {
                    btn.classList.remove('bg-gray-100', 'text-gray-700', 'hover:bg-gray-200');
                    btn.classList.add('bg-green-600', 'text-white');
                } else {
                    btn.classList.add('bg-gray-100', 'text-gray-700', 'hover:bg-gray-200');
                    btn.classList.remove('bg-green-600', 'text-white');
                }
            });
        }
        function toggleStatus(id, activate) {
            if (!confirm('Are you sure?')) return;
            const url = activate
                ? `{{ route('mentorship.programmes.activate', ['programme' => 'ID']) }}`.replace('ID', id)
                : `{{ route('mentorship.programmes.deactivate', ['programme' => 'ID']) }}`.replace('ID', id);
            fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content } })
                .then(r => r.json()).then(d => { if (d.success) location.reload(); else alert(d.message); });
        }
    </script>
    @endpush
</x-app-layout>
