<x-site-layout title="Mentorship Programme" meta-description="Join our mentorship programmes to grow your skills and connect with experienced mentors.">

    {{-- Hero Section — Advocacy Style --}}
    <div class="relative bg-gradient-to-r from-green-700 to-emerald-800 shadow-xl">
        <div class="relative max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
            <div class="text-center">
                <span class="inline-block px-4 py-1 bg-white/20 text-white rounded-full text-sm font-semibold mb-4">
                    <i class="fas fa-graduation-cap mr-2"></i>
                    Mentorship Programme
                </span>
                <h1 class="text-4xl md:text-5xl font-serif font-bold text-white mb-4">
                    Grow With Expert Mentorship
                </h1>
                <p class="text-xl text-green-100 max-w-3xl mx-auto mb-8">
                    Connect with experienced mentors who will guide you on your professional journey. 
                    Transform your career through personalized mentorship.
                </p>
                <div class="flex flex-wrap justify-center gap-4">
                    <a href="#programmes" 
                       class="px-8 py-3 bg-white text-green-800 rounded-lg font-semibold hover:bg-green-50 transition shadow-lg">
                        <i class="fas fa-search mr-2"></i>
                        Browse Programmes
                    </a>
                    <a href="{{ route('register.role.mentee') }}" 
                       class="px-8 py-3 bg-green-500 text-white rounded-lg font-semibold hover:bg-green-400 transition">
                        <i class="fas fa-user-graduate mr-2"></i>
                        Apply Now (Become a Mentee)
                    </a>
                    <a href="{{ route('register.role.mentor') }}" 
                       class="px-8 py-3 bg-white/20 text-white rounded-lg font-semibold hover:bg-white/30 transition backdrop-blur">
                        <i class="fas fa-user-tie mr-2"></i>
                        Become a Mentor
                    </a>
                </div>
            </div>
        </div>
    </div>

    {{-- Breadcrumb --}}
    <div class="bg-gray-50 border-b border-gray-200">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-3">
            <nav class="flex items-center space-x-2 text-sm text-gray-600" aria-label="Breadcrumb">
                <a href="{{ url('/') }}" class="hover:text-green-600 transition flex items-center">
                    <i class="fas fa-home mr-1"></i>Home
                </a>
                <i class="fas fa-chevron-right text-gray-400 text-xs"></i>
                <span class="text-gray-900 font-medium">Mentorship Programme</span>
            </nav>
        </div>
    </div>

    {{-- Featured Programmes --}}
    @if($featuredProgrammes->count() > 0)
        <section class="py-16 bg-white">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="text-center mb-12">
                    <span class="text-green-600 font-semibold text-sm uppercase tracking-wider">Featured</span>
                    <h2 class="text-3xl font-serif font-bold text-gray-900 mt-2">Featured Programmes</h2>
                    <p class="text-gray-600 mt-4 max-w-2xl mx-auto">
                        Our top mentorship programmes, handpicked for exceptional learning experiences
                    </p>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    @foreach($featuredProgrammes as $programme)
                        <div class="group bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg transition group">
                            @if($programme->featured_image)
                                <div class="h-40 overflow-hidden">
                                    <img src="{{ $programme->featured_image }}" alt="{{ $programme->title }}"
                                         class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300">
                                </div>
                            @else
                                <div class="h-40 bg-gradient-to-br from-green-400 to-green-600 flex items-center justify-center">
                                    <i class="fas fa-chalkboard-teacher text-white/20 text-6xl"></i>
                                </div>
                            @endif
                            <div class="p-5">
                                <div class="flex items-center gap-2 mb-2">
                                    <span class="px-2 py-1 bg-green-100 text-green-700 rounded-full text-xs font-medium">
                                        {{ $programme->category->name ?? 'General' }}
                                    </span>
                                    @if($programme->is_free)
                                        <span class="px-2 py-1 bg-blue-100 text-blue-700 rounded-full text-xs font-medium">Free</span>
                                    @endif
                                    <span class="px-2 py-1 bg-yellow-100 text-yellow-700 rounded-full text-xs font-medium">
                                        <i class="fas fa-star mr-1"></i>Featured
                                    </span>
                                </div>
                                <h3 class="text-lg font-bold text-gray-900 mb-2 group-hover:text-green-600 transition">
                                    {{ $programme->title }}
                                </h3>
                                <p class="text-gray-600 text-sm mb-4 line-clamp-2">
                                    {{ Str::limit($programme->description, 100) }}
                                </p>
                                <div class="flex items-center justify-between text-sm text-gray-500 mb-4">
                                    <span><i class="fas fa-clock mr-1"></i> {{ $programme->duration_text }}</span>
                                    <span><i class="fas fa-users mr-1"></i> {{ $programme->applications_count ?? 0 }} applied</span>
                                </div>
                                <div class="flex gap-2">
                                    <a href="{{ route('mentorship.public.programme', $programme) }}" 
                                       class="flex-1 text-center px-4 py-2 border border-green-600 text-green-600 rounded-lg hover:bg-green-50 transition font-medium text-sm">
                                        View Details
                                    </a>
                                    <a href="{{ route('mentorship.public.apply', ['programme_id' => $programme->id]) }}" 
                                       class="flex-1 text-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition font-medium text-sm">
                                        Apply Now
                                    </a>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
        </section>
    @endif

    {{-- Categories Section --}}
    @if($categories->count() > 0)
        <section class="py-16 bg-gray-50">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="text-center mb-12">
                    <span class="text-green-600 font-semibold text-sm uppercase tracking-wider">Categories</span>
                    <h2 class="text-3xl font-serif font-bold text-gray-900 mt-2">Browse by Category</h2>
                </div>

                <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
                    @foreach($categories as $category)
                        <a href="{{ route('mentorship.public.category', $category) }}" 
                           class="group p-6 bg-white rounded-xl shadow hover:shadow-lg transition border border-gray-100 text-center">
                            <div class="w-16 h-16 mx-auto mb-4 bg-green-100 rounded-full flex items-center justify-center group-hover:bg-green-200 transition">
                                <i class="{{ $category->icon ?? 'fas fa-folder' }} text-2xl text-green-600"></i>
                            </div>
                            <h3 class="font-semibold text-gray-900 group-hover:text-green-600 transition">
                                {{ $category->name }}
                            </h3>
                            <p class="text-sm text-gray-500 mt-1">
                                {{ $category->active_programmes_count ?? 0 }} programmes
                            </p>
                        </a>
                    @endforeach
                </div>
            </div>
        </section>
    @endif

    {{-- All Programmes with Real-Time Search --}}
    <section id="programmes" class="py-16 bg-white">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row md:items-center md:justify-between mb-12">
                <div>
                    <h2 class="text-3xl font-serif font-bold text-gray-900">All Programmes</h2>
                    <p class="text-gray-600 mt-2">Find the perfect mentorship programme for your goals</p>
                </div>
            </div>

            {{-- Real-Time Search Bar --}}
            <div class="mb-8" x-data="mentorshipSearch()">
                <div class="bg-gray-50 rounded-xl border border-gray-200 p-4 mb-4">
                    <div class="relative">
                        <i class="fas fa-search absolute left-4 top-1/2 -translate-y-1/2 text-gray-400"></i>
                        <input type="text" 
                               x-model="query"
                               @input.debounce.300ms="search()"
                               x-ref="searchInput"
                               placeholder="Search programmes by title, description..."
                               class="w-full pl-12 pr-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-gray-900">
                        <button x-show="query.length > 0" @click="clearSearch()" 
                                class="absolute right-4 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600">
                            <i class="fas fa-times"></i>
                        </button>
                    </div>

                    {{-- Inline Filters --}}
                    <div class="flex flex-wrap gap-3 mt-3">
                        <select x-model="categoryFilter" @change="search()"
                                class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-green-500 focus:border-green-500 text-sm">
                            <option value="">All Categories</option>
                            @foreach($categories as $cat)
                                <option value="{{ $cat->id }}">{{ $cat->name }}</option>
                            @endforeach
                        </select>
                        <select x-model="feeFilter" @change="search()"
                                class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-green-500 focus:border-green-500 text-sm">
                            <option value="">All Types</option>
                            <option value="1">Free Only</option>
                            <option value="0">Paid Only</option>
                        </select>
                        <select x-model="sessionTypeFilter" @change="search()"
                                class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-green-500 focus:border-green-500 text-sm">
                            <option value="">All Formats</option>
                            <option value="online">Online</option>
                            <option value="in_person">In-Person</option>
                            <option value="hybrid">Hybrid</option>
                        </select>
                        <button @click="resetFilters()" 
                                class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition text-sm">
                            <i class="fas fa-times mr-1"></i> Clear Filters
                        </button>
                    </div>
                </div>

                {{-- Search Results Indicator --}}
                <div x-show="loading" class="text-center py-4">
                    <i class="fas fa-spinner fa-spin text-green-600 text-xl"></i>
                    <span class="ml-2 text-gray-600">Searching...</span>
                </div>

                {{-- Search Results Modal (floating panel) --}}
                <div x-show="hasResults && query.length > 0 && results.length > 0" 
                     x-transition:enter="transition ease-out duration-200"
                     x-transition:enter-start="opacity-0 -translate-y-2"
                     x-transition:enter-end="opacity-100 translate-y-0"
                     x-transition:leave="transition ease-in duration-150"
                     x-transition:leave-start="opacity-100 translate-y-0"
                     x-transition:leave-end="opacity-0 -translate-y-2"
                     class="bg-white rounded-xl shadow-xl border border-gray-200 max-h-96 overflow-y-auto mb-6">
                    <div class="p-3 bg-green-50 border-b border-green-200 flex items-center justify-between sticky top-0">
                        <span class="text-sm font-medium text-green-800">
                            <i class="fas fa-search mr-1"></i>
                            <span x-text="results.length"></span> result(s) found
                        </span>
                        <button @click="clearSearch()" class="text-green-600 hover:text-green-800 text-sm">
                            <i class="fas fa-times mr-1"></i> Close
                        </button>
                    </div>
                    <div class="divide-y divide-gray-100">
                        <template x-for="programme in results" :key="programme.id">
                            <a :href="programme.url" class="block p-4 hover:bg-green-50 transition">
                                <div class="flex items-start justify-between gap-4">
                                    <div class="flex-1 min-w-0">
                                        <div class="flex items-center gap-2 mb-1">
                                            <span class="px-2 py-0.5 bg-green-100 text-green-700 rounded-full text-xs font-medium" 
                                                  x-text="programme.category"></span>
                                            <span class="px-2 py-0.5 text-xs font-medium rounded-full"
                                                  :class="programme.is_free ? 'bg-blue-100 text-blue-700' : 'bg-purple-100 text-purple-700'"
                                                  x-text="programme.is_free ? 'Free' : programme.formatted_fee"></span>
                                        </div>
                                        <h4 class="font-semibold text-gray-900 truncate" x-text="programme.title"></h4>
                                        <p class="text-sm text-gray-500 mt-1 line-clamp-1" x-text="programme.description"></p>
                                        <div class="flex items-center gap-3 mt-2 text-xs text-gray-400">
                                            <span><i class="fas fa-clock mr-1"></i><span x-text="programme.duration_text"></span></span>
                                            <span><i class="fas fa-video mr-1"></i><span x-text="programme.session_type_label"></span></span>
                                        </div>
                                    </div>
                                    <div class="flex-shrink-0 flex gap-2">
                                        <a :href="programme.url" class="px-3 py-1.5 border border-green-600 text-green-600 rounded-lg hover:bg-green-50 transition text-xs font-medium">
                                            Details
                                        </a>
                                        <a :href="programme.apply_url" class="px-3 py-1.5 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-xs font-medium">
                                            Apply
                                        </a>
                                    </div>
                                </div>
                            </a>
                        </template>
                    </div>
                </div>

                {{-- No Results Message --}}
                <div x-show="hasResults && query.length > 0 && results.length === 0" 
                     class="bg-gray-50 rounded-xl border border-gray-200 p-8 text-center mb-6">
                    <i class="fas fa-search text-gray-300 text-4xl mb-3"></i>
                    <h3 class="text-lg font-medium text-gray-900 mb-1">No programmes found</h3>
                    <p class="text-gray-500 text-sm">Try adjusting your search or filters</p>
                </div>

                {{-- Server-side Programmes (default loaded programmes) --}}
                <div x-show="!hasResults || query.length === 0">
                    @if($programmes->count() > 0)
                        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                            @foreach($programmes as $programme)
                                <div class="bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg transition-all duration-300 border border-gray-100">
                                    @if($programme->featured_image)
                                        <div class="h-40 overflow-hidden">
                                            <img src="{{ $programme->featured_image }}" alt="{{ $programme->title }}"
                                                 class="w-full h-full object-cover hover:scale-105 transition-transform duration-300">
                                        </div>
                                    @endif
                                    <div class="p-5">
                                        <div class="flex items-center gap-2 mb-3">
                                            <span class="px-2 py-1 bg-green-100 text-green-700 rounded-full text-xs font-medium">
                                                {{ $programme->category->name ?? 'General' }}
                                            </span>
                                            @if($programme->is_free)
                                                <span class="px-2 py-1 bg-blue-100 text-blue-700 rounded-full text-xs font-medium">Free</span>
                                            @else
                                                <span class="px-2 py-1 bg-purple-100 text-purple-700 rounded-full text-xs font-medium">
                                                    {{ number_format($programme->fee_amount, 0) }} {{ $programme->currency }}
                                                </span>
                                            @endif
                                        </div>
                                        <h3 class="text-lg font-bold text-gray-900 mb-2">
                                            {{ $programme->title }}
                                        </h3>
                                        <p class="text-gray-600 text-sm mb-4 line-clamp-3">
                                            {{ Str::limit($programme->description, 150) }}
                                        </p>
                                        <div class="flex items-center gap-4 text-sm text-gray-500 mb-4">
                                            <span><i class="fas fa-clock mr-1"></i> {{ $programme->duration_text }}</span>
                                            <span><i class="fas fa-video mr-1"></i> {{ ucfirst(str_replace('_', ' ', $programme->session_type)) }}</span>
                                        </div>
                                        <div class="flex gap-2">
                                            <a href="{{ route('mentorship.public.programme', $programme) }}" 
                                               class="flex-1 text-center px-4 py-2 border border-green-600 text-green-600 rounded-lg hover:bg-green-50 transition font-medium text-sm">
                                                Details
                                            </a>
                                            <a href="{{ route('mentorship.public.apply', ['programme_id' => $programme->id]) }}" 
                                               class="flex-1 text-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition font-medium text-sm">
                                                Apply
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>

                        {{-- Pagination --}}
                        <div class="mt-12">
                            {{ $programmes->appends($filters)->links() }}
                        </div>
                    @else
                        <div class="text-center py-16">
                            <i class="fas fa-search text-gray-300 text-6xl mb-4"></i>
                            <h3 class="text-xl font-medium text-gray-900 mb-2">No Programmes Found</h3>
                            <p class="text-gray-500">Try adjusting your filters or check back later.</p>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </section>

    {{-- Why Mentorship Section --}}
    <section class="py-20 bg-gradient-to-r from-green-700 to-emerald-800 text-white">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-16">
                <h2 class="text-3xl font-serif font-bold">Why Join Our Mentorship Programme?</h2>
                <p class="text-green-100 mt-4 max-w-3xl mx-auto">
                    Our mentorship programme connects you with industry experts who can guide your professional growth
                </p>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
                <div class="text-center p-6">
                    <div class="w-16 h-16 mx-auto mb-6 bg-white/20 rounded-full flex items-center justify-center">
                        <i class="fas fa-user-tie text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">Expert Mentors</h3>
                    <p class="text-green-100">
                        Learn from experienced professionals who have excelled in their fields
                    </p>
                </div>
                <div class="text-center p-6">
                    <div class="w-16 h-16 mx-auto mb-6 bg-white/20 rounded-full flex items-center justify-center">
                        <i class="fas fa-comments text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">Personalized Guidance</h3>
                    <p class="text-green-100">
                        One-on-one sessions tailored to your specific goals and challenges
                    </p>
                </div>
                <div class="text-center p-6">
                    <div class="w-16 h-16 mx-auto mb-6 bg-white/20 rounded-full flex items-center justify-center">
                        <i class="fas fa-certificate text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">Recognized Certificates</h3>
                    <p class="text-green-100">
                        Earn certificates upon completion to showcase your achievement
                    </p>
                </div>
            </div>

            <div class="text-center mt-12 flex flex-wrap justify-center gap-4">
                <a href="{{ route('register.role.mentee') }}" 
                   class="px-8 py-4 bg-yellow-400 text-green-900 rounded-xl font-semibold hover:bg-yellow-300 transition shadow-lg">
                    <i class="fas fa-user-graduate mr-2"></i>
                    Start Your Journey as a Mentee
                </a>
                <a href="{{ route('register.role.mentor') }}" 
                   class="px-8 py-4 bg-white/20 backdrop-blur text-white rounded-xl font-semibold hover:bg-white/30 transition shadow-lg">
                    <i class="fas fa-user-tie mr-2"></i>
                    Become a Mentor
                </a>
            </div>
        </div>
    </section>

    {{-- Quick Apply Modal --}}
    <div id="applyModal" class="fixed inset-0 bg-black/50 z-50 hidden overflow-y-auto" x-cloak>
        <div class="min-h-screen flex items-center justify-center p-4">
            <div class="bg-white rounded-2xl w-full max-w-md overflow-hidden">
                <div class="flex items-center justify-between px-6 py-4 bg-gradient-to-r from-green-700 to-emerald-700">
                    <h3 class="text-xl font-bold text-white">Quick Apply</h3>
                    <button onclick="closeApplyModal()" class="text-white/80 hover:text-white transition">
                        <i class="fas fa-times text-xl"></i>
                    </button>
                </div>
                <div class="p-6">
                <p class="text-gray-600 mb-6">Select a programme to begin your application</p>
                
                <div class="space-y-3 max-h-96 overflow-y-auto">
                    @foreach($programmes as $prog)
                        <a href="{{ route('mentorship.public.apply', ['programme_id' => $prog->id]) }}" 
                           class="block p-4 border border-gray-200 rounded-xl hover:border-green-500 hover:bg-green-50 transition">
                            <div class="flex justify-between items-center">
                                <div>
                                    <h4 class="font-semibold text-gray-900">{{ $prog->title }}</h4>
                                    <p class="text-sm text-gray-500">{{ $prog->duration_text }}</p>
                                </div>
                                <div class="text-right">
                                    @if($prog->is_free)
                                        <span class="text-green-600 font-semibold">Free</span>
                                    @else
                                        <span class="text-purple-600 font-semibold">
                                            {{ number_format($prog->fee_amount, 0) }} {{ $prog->currency }}
                                        </span>
                                    @endif
                                </div>
                            </div>
                        </a>
                    @endforeach
                </div>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        function mentorshipSearch() {
            return {
                query: '',
                categoryFilter: '',
                feeFilter: '',
                sessionTypeFilter: '',
                results: [],
                hasResults: false,
                loading: false,
                abortController: null,

                search() {
                    // Cancel previous request if still pending
                    if (this.abortController) {
                        this.abortController.abort();
                    }

                    // If query is empty and no filters, show server-side results
                    if (this.query.length === 0 && !this.categoryFilter && !this.feeFilter && !this.sessionTypeFilter) {
                        this.hasResults = false;
                        this.results = [];
                        return;
                    }

                    this.loading = true;
                    this.abortController = new AbortController();

                    const params = new URLSearchParams();
                    if (this.query) params.set('q', this.query);
                    if (this.categoryFilter) params.set('category_id', this.categoryFilter);
                    if (this.feeFilter !== '') params.set('is_free', this.feeFilter);
                    if (this.sessionTypeFilter) params.set('session_type', this.sessionTypeFilter);

                    fetch('{{ route("mentorship.public.search") }}?' + params.toString(), {
                        headers: {
                            'Accept': 'application/json',
                            'X-Requested-With': 'XMLHttpRequest'
                        },
                        signal: this.abortController.signal
                    })
                    .then(response => response.json())
                    .then(data => {
                        this.results = data.data || [];
                        this.hasResults = true;
                        this.loading = false;
                    })
                    .catch(error => {
                        if (error.name !== 'AbortError') {
                            this.loading = false;
                        }
                    });
                },

                clearSearch() {
                    this.query = '';
                    this.categoryFilter = '';
                    this.feeFilter = '';
                    this.sessionTypeFilter = '';
                    this.hasResults = false;
                    this.results = [];
                    this.$refs.searchInput?.focus();
                },

                resetFilters() {
                    this.categoryFilter = '';
                    this.feeFilter = '';
                    this.sessionTypeFilter = '';
                    if (this.query.length > 0) {
                        this.search();
                    } else {
                        this.hasResults = false;
                        this.results = [];
                    }
                }
            }
        }

        function openApplyModal() {
            document.getElementById('applyModal').classList.remove('hidden');
            document.body.style.overflow = 'hidden';
        }

        function closeApplyModal() {
            document.getElementById('applyModal').classList.add('hidden');
            document.body.style.overflow = '';
        }

        document.getElementById('applyModal')?.addEventListener('click', function(e) {
            if (e.target === this) closeApplyModal();
        });
    </script>
    @endpush
</x-site-layout>
