<x-app-layout>

    <x-module-hero
        title="Knowledge Base Categories"
        description="Manage and organise knowledge base content categories"
        icon="fa-book-open">

        <x-slot name="tabs">
            @can('view discussion topics')
            <a href="{{ route('knowledge-sharing.discussions.topics.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.topics.*') ? '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-lightbulb text-sm"></i> Discussion Topics
            </a>
            @endcan
            @can('view discussions')
            <a href="{{ route('knowledge-sharing.discussions.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.index') || (request()->routeIs('knowledge-sharing.discussions.*') && !request()->routeIs('knowledge-sharing.discussions.topics.*') && !request()->routeIs('knowledge-sharing.discussions.replies.*')) ? '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-comments text-sm"></i> Discussions
            </a>
            @endcan
            @if(Route::has('knowledge-sharing.discussions.replies.index'))
            @can('view discussion replies')
            <a href="{{ route('knowledge-sharing.discussions.replies.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.replies.*') ? '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-reply-all text-sm"></i> All Replies
            </a>
            @endcan
            @endif
            @can('manage knowledge base categories')
            <a href="{{ route('knowledge-sharing.knowledge-base.categories.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.knowledge-base.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-book-open text-sm"></i> KB Categories
            </a>
            @endcan
            @can('view knowledge base questions')
            <a href="{{ route('knowledge-sharing.knowledge-base.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.knowledge-base.index') || (request()->routeIs('knowledge-sharing.knowledge-base.*') && !request()->routeIs('knowledge-sharing.knowledge-base.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-circle-question text-sm"></i> Knowledge Base Q&amp;A
            </a>
            @endcan
            @hasRole('Contributor')
            <a href="{{ route('contributor.dashboard') }}"
               class="module-hero-tab {{ request()->routeIs('contributor.dashboard') ? '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-pen-nib text-sm"></i> Contributor Dashboard
            </a>
            @endhasRole
        </x-slot>

        @can('create knowledge base categories')
        <x-slot name="actions">
            <a href="{{ route('knowledge-sharing.knowledge-base.categories.create') }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-white text-[#1a4731] text-sm font-semibold rounded-lg hover:bg-green-50 transition-colors">
                <i class="fas fa-plus"></i> New Category
            </a>
        </x-slot>
        @endcan

    </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

        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">

            {{-- Filter --}}
            <div class="p-4 bg-gray-50 border-b border-gray-200">
                <form method="GET" class="flex flex-col sm:flex-row gap-3">
                    <div class="flex-1 relative">
                        <input type="text" name="search" value="{{ request('search') }}"
                               placeholder="Search categories…"
                               class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                        <i class="fas fa-search absolute left-3 top-1/2 -translate-y-1/2 text-gray-400 text-sm"></i>
                    </div>
                    <label class="inline-flex items-center gap-2 px-3 py-2 border border-gray-300 rounded-lg bg-white text-sm cursor-pointer">
                        <input type="checkbox" name="inactive" value="1" {{ request('inactive') ? 'checked' : '' }} class="rounded">
                        Include Inactive
                    </label>
                    <button type="submit" class="px-4 py-2 bg-green-600 text-white text-sm font-medium rounded-lg hover:bg-green-700 transition-colors">
                        <i class="fas fa-filter mr-1"></i> Filter
                    </button>
                </form>
            </div>

            {{-- Categories Tree --}}
            <div class="p-6 space-y-4">
                @forelse($categories as $category)
                    <div class="border border-gray-200 rounded-lg overflow-hidden {{ $category->is_active ? '' : 'opacity-60' }}">

                        {{-- Root Category Row --}}
                        <div class="flex items-center justify-between px-4 py-3 bg-gray-50">
                            <div class="flex items-center gap-3">
                                <div class="w-9 h-9 rounded-lg bg-green-100 flex items-center justify-center flex-shrink-0">
                                    <i class="fas {{ $category->icon ?? 'fa-folder' }} text-green-600"></i>
                                </div>
                                <div>
                                    <a href="{{ route('knowledge-sharing.knowledge-base.categories.show', $category) }}"
                                       class="font-semibold text-gray-900 hover:text-green-600 transition-colors">
                                        {{ $category->name }}
                                    </a>
                                    <p class="text-xs text-gray-400 font-mono">{{ $category->slug }}</p>
                                </div>
                            </div>
                            <div class="flex items-center gap-4">
                                <div class="flex items-center gap-3 text-xs text-gray-500">
                                    <span title="Questions"><i class="fas fa-circle-question mr-1"></i>{{ $category->questions_count ?? 0 }}</span>
                                    <span title="Subcategories"><i class="fas fa-folder mr-1"></i>{{ $category->children->count() }}</span>
                                </div>
                                @if(!$category->is_active)
                                    <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-red-100 text-red-800">Inactive</span>
                                @endif
                                <div class="flex items-center gap-1">
                                    <a href="{{ route('knowledge-sharing.knowledge-base.categories.show', $category) }}" class="btn-action-view">
                                        <i class="fas fa-eye"></i> View
                                    </a>
                                    @can('edit knowledge base categories')
                                    <a href="{{ route('knowledge-sharing.knowledge-base.categories.edit', $category) }}" class="btn-action-edit">
                                        <i class="fas fa-edit"></i> Edit
                                    </a>
                                    <form action="{{ route('knowledge-sharing.knowledge-base.categories.toggle-active', $category) }}" method="POST" class="inline">
                                        @csrf
                                        <button type="submit" class="{{ $category->is_active ? 'btn-action-reject' : 'btn-action-approve' }}">
                                            <i class="fas {{ $category->is_active ? 'fa-toggle-off' : 'fa-toggle-on' }}"></i>
                                        </button>
                                    </form>
                                    @endcan
                                    @can('delete knowledge base categories')
                                    <form action="{{ route('knowledge-sharing.knowledge-base.categories.destroy', $category) }}" method="POST" class="inline"
                                          onsubmit="return confirm('Delete this category?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn-action-delete">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                    @endcan
                                </div>
                            </div>
                        </div>

                        {{-- Subcategories --}}
                        @if($category->children->isNotEmpty())
                            <div class="border-t border-gray-200 divide-y divide-gray-100">
                                @foreach($category->children as $child)
                                    <div class="flex items-center justify-between px-4 py-2.5 pl-14 bg-white {{ $child->is_active ? '' : 'opacity-60' }}">
                                        <div class="flex items-center gap-2">
                                            <div class="w-6 h-6 rounded bg-gray-100 flex items-center justify-center flex-shrink-0">
                                                <i class="fas {{ $child->icon ?? 'fa-folder' }} text-gray-500 text-xs"></i>
                                            </div>
                                            <span class="text-sm text-gray-800">{{ $child->name }}</span>
                                            <span class="text-xs text-gray-400">({{ $child->questions_count ?? 0 }} questions)</span>
                                        </div>
                                        <div class="flex items-center gap-1">
                                            @can('edit knowledge base categories')
                                            <a href="{{ route('knowledge-sharing.knowledge-base.categories.edit', $child) }}" class="btn-action-edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            @endcan
                                            @can('delete knowledge base categories')
                                            <form action="{{ route('knowledge-sharing.knowledge-base.categories.destroy', $child) }}" method="POST" class="inline"
                                                  onsubmit="return confirm('Delete this subcategory?')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn-action-delete">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                            @endcan
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        @endif

                    </div>
                @empty
                    <div class="text-center py-12">
                        <i class="fas fa-folder-open text-4xl text-gray-300 mb-3 block"></i>
                        <p class="text-gray-500 mb-4">No categories found.</p>
                        @can('create knowledge base categories')
                        <a href="{{ route('knowledge-sharing.knowledge-base.categories.create') }}"
                           class="inline-flex items-center gap-2 px-4 py-2 bg-green-600 text-white text-sm rounded-lg hover:bg-green-700">
                            <i class="fas fa-plus"></i> Create Category
                        </a>
                        @endcan
                    </div>
                @endforelse
            </div>

        </div>
    </div>

</x-app-layout>
