<x-app-layout>
    <x-module-hero
        :title="$category->name"
        :description="($category->programmes_count ?? 0) . ' programmes'"
        icon="fa-folder-open">

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

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            {{-- Main Content --}}
            <div class="lg:col-span-2 space-y-4">
                {{-- Category Details --}}
                <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> Category Details
                    </h3>
                    <div class="space-y-4 text-sm">
                        @if($category->description)
                            <div>
                                <h4 class="text-xs font-medium text-gray-500 mb-1">Description</h4>
                                <p class="text-gray-900">{{ $category->description }}</p>
                            </div>
                        @endif

                        @if($category->parent)
                            <div>
                                <h4 class="text-xs font-medium text-gray-500 mb-1">Parent Category</h4>
                                <a href="{{ route('mentorship.categories.show', $category->parent) }}"
                                   class="text-green-600 hover:text-green-800 inline-flex items-center gap-1">
                                    <i class="fas fa-folder text-xs"></i>
                                    {{ $category->parent->name }}
                                </a>
                            </div>
                        @endif

                        @if($category->icon)
                            <div>
                                <h4 class="text-xs font-medium text-gray-500 mb-1">Icon</h4>
                                <span class="inline-flex items-center gap-2">
                                    <i class="{{ $category->icon }} text-green-600"></i>
                                    <code class="text-xs bg-gray-100 px-2 py-0.5 rounded">{{ $category->icon }}</code>
                                </span>
                            </div>
                        @endif

                        @if($category->children->count() > 0)
                            <div>
                                <h4 class="text-xs font-medium text-gray-500 mb-2">Sub-Categories</h4>
                                <div class="flex flex-wrap gap-2">
                                    @foreach($category->children as $child)
                                        <a href="{{ route('mentorship.categories.show', $child) }}"
                                           class="inline-flex items-center gap-1 px-3 py-1 bg-green-50 text-green-700 rounded-full text-xs hover:bg-green-100 transition">
                                            <i class="fas fa-folder text-xs"></i>
                                            {{ $child->name }}
                                            <span class="text-green-500">({{ $child->programmes_count ?? 0 }})</span>
                                        </a>
                                    @endforeach
                                </div>
                            </div>
                        @endif
                    </div>
                </div>

                {{-- Programmes in this category --}}
                <div class="bg-white rounded-lg border border-gray-200">
                    <div class="p-4 border-b border-gray-200 flex items-center justify-between">
                        <h3 class="text-base font-semibold text-gray-800 flex items-center gap-2">
                            <i class="fas fa-chalkboard-teacher text-green-600"></i> Programmes in this Category
                        </h3>
                        @can('view mentorship programmes')
                            <a href="{{ route('mentorship.programmes.index', ['category_id' => $category->id]) }}"
                               class="text-green-600 hover:text-green-800 text-xs">
                                View All <i class="fas fa-arrow-right ml-1"></i>
                            </a>
                        @endcan
                    </div>

                    @if($category->programmes->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">Title</th>
                                        <th class="th-col-2 px-4 py-3 text-left">Fee</th>
                                        <th class="th-col-3 px-4 py-3 text-left">Status</th>
                                        <th class="th-col-4 px-4 py-3 text-center">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($category->programmes as $programme)
                                        <tr>
                                            <td class="px-4 py-3 text-sm font-medium text-gray-900">{{ $programme->title }}</td>
                                            <td class="px-4 py-3 text-sm">
                                                @if($programme->is_free)
                                                    <span class="text-green-600">Free</span>
                                                @else
                                                    {{ number_format($programme->fee_amount, 2) }} {{ $programme->currency }}
                                                @endif
                                            </td>
                                            <td class="px-4 py-3">
                                                @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
                                            </td>
                                            <td class="px-4 py-3 text-center">
                                                @can('view mentorship programmes')
                                                    <a href="{{ route('mentorship.programmes.show', $programme) }}" class="btn-action-view">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                @endcan
                                            </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 programmes in this category yet
                        </div>
                    @endif
                </div>
            </div>

            {{-- Sidebar --}}
            <div class="space-y-4">
                {{-- Quick Info --}}
                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <h3 class="text-base font-semibold text-gray-800 mb-4">Quick Info</h3>
                    <dl class="space-y-3 text-sm">
                        <div class="flex justify-between">
                            <dt class="text-gray-500">Status</dt>
                            <dd>
                                @if($category->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">Sort Order</dt>
                            <dd class="font-medium">{{ $category->sort_order }}</dd>
                        </div>
                        @if($category->color)
                            <div class="flex justify-between items-center">
                                <dt class="text-gray-500">Color</dt>
                                <dd class="flex items-center gap-2">
                                    <span class="w-4 h-4 rounded-full border border-gray-200 inline-block" style="background-color: {{ $category->color }}"></span>
                                    <code class="text-xs">{{ $category->color }}</code>
                                </dd>
                            </div>
                        @endif
                        <div class="flex justify-between">
                            <dt class="text-gray-500">Created</dt>
                            <dd class="font-medium">{{ $category->created_at->format('M d, Y') }}</dd>
                        </div>
                    </dl>
                </div>

                {{-- Statistics --}}
                <div class="bg-white rounded-lg border border-gray-200 p-6">
                    <h3 class="text-base font-semibold text-gray-800 mb-4">Statistics</h3>
                    <div class="grid grid-cols-2 gap-3">
                        <div class="text-center p-3 bg-blue-50 rounded-lg">
                            <div class="text-2xl font-bold text-blue-600">{{ $statistics['programmes'] ?? 0 }}</div>
                            <div class="text-xs text-gray-500">Programmes</div>
                        </div>
                        <div class="text-center p-3 bg-green-50 rounded-lg">
                            <div class="text-2xl font-bold text-green-600">{{ $statistics['active_programmes'] ?? 0 }}</div>
                            <div class="text-xs text-gray-500">Active</div>
                        </div>
                        <div class="text-center p-3 bg-purple-50 rounded-lg">
                            <div class="text-2xl font-bold text-purple-600">{{ $statistics['children'] ?? 0 }}</div>
                            <div class="text-xs text-gray-500">Sub-categories</div>
                        </div>
                        <div class="text-center p-3 bg-yellow-50 rounded-lg">
                            <div class="text-2xl font-bold text-yellow-600">{{ $statistics['applications'] ?? 0 }}</div>
                            <div class="text-xs text-gray-500">Applications</div>
                        </div>
                    </div>
                </div>

                {{-- Actions --}}
                @canany(['edit mentorship categories', 'delete mentorship categories'])
                    <div class="bg-white rounded-lg border border-gray-200 p-6">
                        <h3 class="text-base font-semibold text-gray-800 mb-3">Actions</h3>
                        <div class="space-y-2">
                            @can('edit mentorship categories')
                                <a href="{{ route('mentorship.categories.edit', $category) }}"
                                   class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-amber-500 text-white rounded-lg hover:bg-amber-600 transition text-sm">
                                    <i class="fas fa-edit"></i> Edit Category
                                </a>
                                @if($category->is_active)
                                    <form action="{{ route('mentorship.categories.deactivate', $category) }}" method="POST">
                                        @csrf
                                        <button type="submit"
                                                class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-yellow-100 text-yellow-800 rounded-lg hover:bg-yellow-200 transition text-sm">
                                            <i class="fas fa-pause"></i> Deactivate
                                        </button>
                                    </form>
                                @else
                                    <form action="{{ route('mentorship.categories.activate', $category) }}" method="POST">
                                        @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-play"></i> Activate
                                        </button>
                                    </form>
                                @endif
                            @endcan
                            @can('delete mentorship categories')
                                @if($category->canBeDeleted())
                                    <form action="{{ route('mentorship.categories.destroy', $category) }}" method="POST"
                                          onsubmit="return confirm('Delete this category? This cannot be undone.')">
                                        @csrf @method('DELETE')
                                        <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-trash"></i> Delete Category
                                        </button>
                                    </form>
                                @else
                                    <p class="text-xs text-gray-500 text-center">
                                        <i class="fas fa-lock mr-1"></i>
                                        Cannot delete — has sub-categories or programmes
                                    </p>
                                @endif
                            @endcan
                        </div>
                    </div>
                @endcanany
            </div>
        </div>
    </div>
</x-app-layout>
