<x-app-layout>

    <x-module-hero
        title="Knowledge Base Q&amp;A"
        description="Manage knowledge base questions and answers"
        icon="fa-circle-question">

        <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 questions')
        <x-slot name="actions">
            <a href="{{ route('knowledge-sharing.knowledge-base.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 Question
            </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

        {{-- Statistics Cards (Phase 12.2) --}}
        @if(isset($statistics))
        <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-4 flex items-center gap-3">
                <div class="w-10 h-10 rounded-xl bg-blue-100 flex items-center justify-center flex-shrink-0">
                    <i class="fas fa-circle-question text-blue-600"></i>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($statistics['published_questions'] ?? 0) }}</p>
                    <p class="text-xs text-gray-500">Published Questions</p>
                </div>
            </div>
            <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-4 flex items-center gap-3">
                <div class="w-10 h-10 rounded-xl bg-green-100 flex items-center justify-center flex-shrink-0">
                    <i class="fas fa-check-circle text-green-600"></i>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($statistics['accepted_answers'] ?? 0) }}</p>
                    <p class="text-xs text-gray-500">Accepted Answers</p>
                </div>
            </div>
            <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-4 flex items-center gap-3">
                <div class="w-10 h-10 rounded-xl bg-purple-100 flex items-center justify-center flex-shrink-0">
                    <i class="fas fa-lightbulb text-purple-600"></i>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($statistics['total_answers'] ?? 0) }}</p>
                    <p class="text-xs text-gray-500">Total Answers</p>
                </div>
            </div>
            <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-4 flex items-center gap-3">
                <div class="w-10 h-10 rounded-xl bg-orange-100 flex items-center justify-center flex-shrink-0">
                    <i class="fas fa-eye text-orange-600"></i>
                </div>
                <div>
                    <p class="text-2xl font-bold text-gray-800">{{ number_format($statistics['total_views'] ?? 0) }}</p>
                    <p class="text-xs text-gray-500">Total Views</p>
                </div>
            </div>
        </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 flex-wrap gap-3">
                    <div class="flex-1 min-w-48 relative">
                        <input type="text" name="search" value="{{ request('search') }}"
                               placeholder="Search questions…"
                               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>
                    <select name="category_id" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                        <option value="">All Categories</option>
                        @foreach($categories as $cat)
                            <option value="{{ $cat->id }}" {{ request('category_id') == $cat->id ? 'selected' : '' }}>{{ $cat->name }}</option>
                        @endforeach
                    </select>
                    <select name="status" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                        <option value="">All Status</option>
                        <option value="published" {{ request('status') === 'published' ? 'selected' : '' }}>Published</option>
                        <option value="draft" {{ request('status') === 'draft' ? 'selected' : '' }}>Draft</option>
                        <option value="pending_review" {{ request('status') === 'pending_review' ? 'selected' : '' }}>Pending Review</option>
                        <option value="archived" {{ request('status') === 'archived' ? 'selected' : '' }}>Archived</option>
                    </select>
                    <select name="difficulty" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                        <option value="">All Difficulties</option>
                        <option value="beginner" {{ request('difficulty') === 'beginner' ? 'selected' : '' }}>Beginner</option>
                        <option value="intermediate" {{ request('difficulty') === 'intermediate' ? 'selected' : '' }}>Intermediate</option>
                        <option value="advanced" {{ request('difficulty') === 'advanced' ? 'selected' : '' }}>Advanced</option>
                    </select>
                    <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>

            <div class="overflow-x-auto">
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="th-col-1 px-4 py-3 text-left">Question</th>
                            <th class="th-col-2 px-4 py-3 text-left">Category</th>
                            <th class="th-col-3 px-4 py-3 text-left">Author</th>
                            <th class="th-col-4 px-4 py-3 text-center">Status</th>
                            <th class="th-col-5 px-4 py-3 text-center">Difficulty</th>
                            <th class="th-col-6 px-4 py-3 text-center">Stats</th>
                            <th class="th-col-7 px-4 py-3 text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($questions as $question)
                            <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0 {{ $question->status === 'archived' ? 'opacity-60' : '' }}">
                                <td class="px-4 py-3">
                                    <div class="flex items-center gap-2">
                                        @if($question->is_featured)
                                            <i class="fas fa-star text-yellow-500 text-xs" title="Featured"></i>
                                        @endif
                                        <div>
                                            <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}"
                                               class="font-medium text-gray-900 hover:text-green-600 transition-colors">
                                                {{ \Illuminate\Support\Str::limit($question->title, 55) }}
                                            </a>
                                            @if($question->tags)
                                                <div class="flex flex-wrap gap-1 mt-1">
                                                    @foreach(array_slice(is_array($question->tags) ? $question->tags : explode(',', $question->tags), 0, 3) as $tag)
                                                        <span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs bg-gray-100 text-gray-600">{{ trim($tag) }}</span>
                                                    @endforeach
                                                </div>
                                            @endif
                                            <p class="text-xs text-gray-400 mt-0.5">{{ $question->created_at->diffForHumans() }}</p>
                                        </div>
                                    </div>
                                </td>
                                <td class="px-4 py-3">
                                    @if($question->category)
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                            {{ $question->category->name }}
                                        </span>
                                    @else
                                        <span class="text-gray-400 text-xs">Uncategorized</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-700">{{ $question->user->name }}</td>
                                <td class="px-4 py-3 text-center">
                                    @php
                                        $sc = ['published'=>'bg-green-100 text-green-800','draft'=>'bg-gray-100 text-gray-700','pending_review'=>'bg-yellow-100 text-yellow-800','archived'=>'bg-red-100 text-red-800'];
                                    @endphp
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $sc[$question->status] ?? 'bg-gray-100 text-gray-700' }}">
                                        {{ ucfirst(str_replace('_', ' ', $question->status)) }}
                                    </span>
                                </td>
                                <td class="px-4 py-3 text-center">
                                    @php
                                        $dc = ['beginner'=>'bg-green-100 text-green-800','intermediate'=>'bg-yellow-100 text-yellow-800','advanced'=>'bg-red-100 text-red-800'];
                                    @endphp
                                    @if($question->difficulty)
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $dc[$question->difficulty] ?? 'bg-gray-100 text-gray-700' }}">
                                            {{ ucfirst($question->difficulty) }}
                                        </span>
                                    @else
                                        <span class="text-gray-400 text-xs">—</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-center">
                                    <div class="flex items-center justify-center gap-3 text-xs text-gray-500">
                                        <span title="Views"><i class="fas fa-eye mr-1"></i>{{ number_format($question->view_count) }}</span>
                                        <span title="Answers"><i class="fas fa-comment mr-1"></i>{{ $question->published_answers_count ?? 0 }}</span>
                                        <span title="Helpful"><i class="fas fa-thumbs-up mr-1"></i>{{ $question->helpful_count }}</span>
                                    </div>
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex items-center justify-center gap-1">
                                        <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}" class="btn-action-view">
                                            <i class="fas fa-eye"></i> View
                                        </a>
                                        @can('edit knowledge base questions')
                                        <a href="{{ route('knowledge-sharing.knowledge-base.edit', $question) }}" class="btn-action-edit">
                                            <i class="fas fa-edit"></i> Edit
                                        </a>
                                        @endcan
                                        @can('publish knowledge base questions')
                                        @if($question->status !== 'published')
                                        <form action="{{ route('knowledge-sharing.knowledge-base.publish', $question) }}" method="POST" class="inline">
                                            @csrf
                                            <button type="submit" class="btn-action-approve">
                                                <i class="fas fa-paper-plane"></i>
                                            </button>
                                        </form>
                                        @endif
                                        @endcan
                                        @can('delete knowledge base questions')
                                        <form action="{{ route('knowledge-sharing.knowledge-base.destroy', $question) }}" method="POST" class="inline"
                                              onsubmit="return confirm('Delete this question?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn-action-delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                                <td colspan="7" class="px-4 py-12 text-center">
                                    <i class="fas fa-circle-question text-4xl text-gray-300 mb-3 block"></i>
                                    <p class="text-gray-500 mb-4">No questions found.</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> Create Question
                                    </a>
                                    @endcan
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if($questions->hasPages())
                <div class="px-4 py-3 border-t border-gray-200">
                    {{ $questions->links() }}
                </div>
            @endif

        </div>
    </div>

</x-app-layout>
