<x-app-layout>

    <x-module-hero
        title="{{ $category->name }}"
        description="{{ $category->description ?? 'Knowledge base category details' }}"
        icon="{{ $category->icon ?? 'fa-book-open' }}">

        <x-slot name="backButton">
            <a href="{{ route('knowledge-sharing.knowledge-base.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 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('edit knowledge base categories')
        <x-slot name="actions">
            <a href="{{ route('knowledge-sharing.knowledge-base.categories.edit', $category) }}"
               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-edit"></i> Edit 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

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">

            {{-- Main Content --}}
            <div class="lg:col-span-2 space-y-6">

                {{-- Stats --}}
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                    <div class="bg-white rounded-lg border border-gray-200 p-4 text-center">
                        <p class="text-2xl font-bold text-gray-800">{{ $category->questions_count ?? 0 }}</p>
                        <p class="text-xs text-gray-500 mt-1">Questions</p>
                    </div>
                    <div class="bg-white rounded-lg border border-gray-200 p-4 text-center">
                        <p class="text-2xl font-bold text-gray-800">{{ $category->published_questions_count ?? 0 }}</p>
                        <p class="text-xs text-gray-500 mt-1">Published</p>
                    </div>
                    <div class="bg-white rounded-lg border border-gray-200 p-4 text-center">
                        <p class="text-2xl font-bold text-gray-800">{{ $category->children->count() }}</p>
                        <p class="text-xs text-gray-500 mt-1">Subcategories</p>
                    </div>
                    <div class="bg-white rounded-lg border border-gray-200 p-4 text-center">
                        <p class="text-2xl font-bold text-gray-800">{{ number_format($category->questions()->sum('view_count')) }}</p>
                        <p class="text-xs text-gray-500 mt-1">Total Views</p>
                    </div>
                </div>

                {{-- Subcategories --}}
                @if($category->children->isNotEmpty())
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-800">
                            <i class="fas fa-sitemap text-green-600 mr-2"></i>
                            Subcategories ({{ $category->children->count() }})
                        </h3>
                    </div>
                    <div class="p-4 grid grid-cols-1 md:grid-cols-2 gap-3">
                        @foreach($category->children as $child)
                            <a href="{{ route('knowledge-sharing.knowledge-base.categories.show', $child) }}"
                               class="flex items-center gap-3 p-3 rounded-lg bg-gray-50 hover:bg-green-50 transition-colors">
                                <div class="w-8 h-8 rounded-lg bg-green-100 flex items-center justify-center flex-shrink-0">
                                    <i class="fas {{ $child->icon ?? 'fa-folder' }} text-green-600 text-sm"></i>
                                </div>
                                <div>
                                    <p class="font-medium text-gray-800 text-sm">{{ $child->name }}</p>
                                    <p class="text-xs text-gray-400">{{ $child->questions_count ?? 0 }} questions</p>
                                </div>
                            </a>
                        @endforeach
                    </div>
                </div>
                @endif

                {{-- Questions --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
                        <h3 class="text-base font-semibold text-gray-800">
                            <i class="fas fa-circle-question text-green-600 mr-2"></i>
                            Questions ({{ $category->questions->count() }})
                        </h3>
                        @can('create knowledge base questions')
                        <a href="{{ route('knowledge-sharing.knowledge-base.create') }}?category={{ $category->id }}"
                           class="text-sm text-green-600 hover:text-green-700">
                            <i class="fas fa-plus mr-1"></i> Add Question
                        </a>
                        @endcan
                    </div>
                    @if($category->questions->isNotEmpty())
                        <div class="divide-y divide-gray-100">
                            @foreach($category->questions as $question)
                                <div class="p-4 hover:bg-gray-50 transition-colors">
                                    <div class="flex items-start justify-between gap-4">
                                        <div class="flex-1 min-w-0">
                                            <div class="flex items-center gap-2">
                                                @if($question->is_featured)
                                                    <i class="fas fa-star text-yellow-500 text-xs"></i>
                                                @endif
                                                <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}"
                                                   class="font-medium text-gray-900 hover:text-green-600 transition-colors">
                                                    {{ $question->title }}
                                                </a>
                                            </div>
                                            <div class="flex items-center gap-3 mt-1 text-xs text-gray-400">
                                                <span>{{ $question->created_at->diffForHumans() }}</span>
                                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium
                                                    {{ $question->status === 'published' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-700' }}">
                                                    {{ ucfirst($question->status) }}
                                                </span>
                                                @if($question->difficulty)
                                                    <span>{{ ucfirst($question->difficulty) }}</span>
                                                @endif
                                            </div>
                                        </div>
                                        <div class="flex items-center gap-3 text-xs text-gray-400 flex-shrink-0">
                                            <span><i class="fas fa-eye mr-1"></i>{{ number_format($question->view_count) }}</span>
                                            <span><i class="fas fa-comment mr-1"></i>{{ $question->answers()->count() }}</span>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @else
                        <div class="p-10 text-center">
                            <i class="fas fa-inbox text-4xl text-gray-300 mb-3 block"></i>
                            <p class="text-gray-500 mb-4">No questions in this category yet.</p>
                            @can('create knowledge base questions')
                            <a href="{{ route('knowledge-sharing.knowledge-base.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> Add First Question
                            </a>
                            @endcan
                        </div>
                    @endif
                </div>

            </div>

            {{-- Sidebar --}}
            <div class="space-y-6">

                {{-- Category Info --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-4">
                        <i class="fas fa-info-circle text-blue-500 mr-2"></i> Category Info
                    </h3>
                    <dl class="space-y-3 text-sm">
                        <div>
                            <dt class="text-gray-500">Status</dt>
                            <dd class="mt-0.5">
                                @if($category->is_active)
                                    <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800">Active</span>
                                @else
                                    <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
                            </dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Parent</dt>
                            <dd class="text-gray-800 mt-0.5">
                                @if($category->parent)
                                    <a href="{{ route('knowledge-sharing.knowledge-base.categories.show', $category->parent) }}"
                                       class="text-green-600 hover:underline">{{ $category->parent->name }}</a>
                                @else
                                    None (Root)
                                @endif
                            </dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Slug</dt>
                            <dd class="font-mono text-gray-800 text-xs mt-0.5">{{ $category->slug }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Sort Order</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $category->sort_order }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Created</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $category->created_at->format('M d, Y') }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Updated</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $category->updated_at->diffForHumans() }}</dd>
                        </div>
                    </dl>
                </div>

                {{-- Actions --}}
                @can('edit knowledge base categories')
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-4">
                        <i class="fas fa-bolt text-green-600 mr-2"></i> Actions
                    </h3>
                    <div class="space-y-2">
                        <a href="{{ route('knowledge-sharing.knowledge-base.categories.edit', $category) }}"
                           class="flex items-center gap-3 px-3 py-2 rounded-lg hover:bg-gray-50 transition-colors text-sm text-gray-700">
                            <i class="fas fa-edit text-amber-500 w-4"></i> Edit Category
                        </a>
                        <form action="{{ route('knowledge-sharing.knowledge-base.categories.toggle-active', $category) }}" method="POST">
                            @csrf
                            <button type="submit"
                                    class="flex items-center gap-3 w-full px-3 py-2 rounded-lg hover:bg-gray-50 transition-colors text-sm text-gray-700 text-left">
                                <i class="fas {{ $category->is_active ? 'fa-toggle-off text-red-500' : 'fa-toggle-on text-green-500' }} w-4"></i>
                                {{ $category->is_active ? 'Deactivate' : 'Activate' }}
                            </button>
                        </form>
                        @can('delete knowledge base categories')
                        @if(!$category->questions()->exists() && !$category->children()->exists())
                        <form action="{{ route('knowledge-sharing.knowledge-base.categories.destroy', $category) }}" method="POST"
                              onsubmit="return confirm('Delete this category?')">
                            @csrf
                            @method('DELETE')
                            <button type="submit"
                                    class="flex items-center gap-3 w-full px-3 py-2 rounded-lg hover:bg-red-50 text-red-600 transition-colors text-sm text-left">
                                <i class="fas fa-trash w-4"></i> Delete Category
                            </button>
                        </form>
                        @endif
                        @endcan
                    </div>
                </div>
                @endcan

                {{-- Public Link --}}
                @if($category->is_active)
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                    <a href="{{ route('public.knowledge-base.category', $category->slug) }}" target="_blank"
                       class="flex items-center justify-center gap-2 w-full px-4 py-2.5 bg-green-600 text-white text-sm font-medium rounded-lg hover:bg-green-700 transition-colors">
                        <i class="fas fa-external-link-alt"></i> View on Public Site
                    </a>
                </div>
                @endif

            </div>
        </div>
    </div>

</x-app-layout>
