<x-app-layout>

    <x-module-hero
        title="Discussion Topics"
        description="Manage and organise discussion topic categories"
        icon="fa-lightbulb">

        <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 discussion topics')
        <x-slot name="actions">
            <a href="{{ route('knowledge-sharing.discussions.topics.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 Topic
            </a>
        </x-slot>
        @endcan

    </x-module-hero>

    <div class="p-6">

        {{-- Flash Messages --}}
        @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">

            {{-- Search / 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 topics…"
                               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-4 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>

            {{-- Table --}}
            <div class="overflow-x-auto">
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="th-col-1 px-4 py-3 text-left">Order</th>
                            <th class="th-col-2 px-4 py-3 text-left">Topic</th>
                            <th class="th-col-3 px-4 py-3 text-left">Description</th>
                            <th class="th-col-4 px-4 py-3 text-center">Discussions</th>
                            <th class="th-col-5 px-4 py-3 text-center">Status</th>
                            <th class="th-col-6 px-4 py-3 text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($topics as $topic)
                            <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0 {{ $topic->is_active ? '' : 'opacity-60' }}">
                                <td class="px-4 py-3 text-sm text-gray-600">
                                    <i class="fas fa-grip-vertical text-gray-400 mr-1"></i>
                                    {{ $topic->sort_order }}
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex items-center gap-3">
                                        <div class="w-8 h-8 rounded-lg bg-green-100 flex items-center justify-center flex-shrink-0">
                                            <i class="fas {{ $topic->icon ?? 'fa-folder' }} text-green-600 text-sm"></i>
                                        </div>
                                        <div>
                                            <a href="{{ route('knowledge-sharing.discussions.topics.show', $topic) }}"
                                               class="font-medium text-gray-900 hover:text-green-600 transition-colors">
                                                {{ $topic->name }}
                                            </a>
                                            <p class="text-xs text-gray-400 font-mono">{{ $topic->slug }}</p>
                                        </div>
                                    </div>
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-600 max-w-xs">
                                    {{ $topic->description ? \Illuminate\Support\Str::limit($topic->description, 60) : '—' }}
                                </td>
                                <td class="px-4 py-3 text-center">
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                        {{ $topic->active_discussions_count ?? 0 }}
                                    </span>
                                </td>
                                <td class="px-4 py-3 text-center">
                                    @if($topic->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
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex items-center justify-center gap-1">
                                        <a href="{{ route('knowledge-sharing.discussions.topics.show', $topic) }}"
                                           class="btn-action-view">
                                            <i class="fas fa-eye"></i> View
                                        </a>
                                        @can('edit discussion topics')
                                        <a href="{{ route('knowledge-sharing.discussions.topics.edit', $topic) }}"
                                           class="btn-action-edit">
                                            <i class="fas fa-edit"></i> Edit
                                        </a>
                                        <form action="{{ route('knowledge-sharing.discussions.topics.toggle-active', $topic) }}" method="POST" class="inline">
                                            @csrf
                                            <button type="submit"
                                                    class="{{ $topic->is_active ? 'btn-action-reject' : 'btn-action-approve' }}">
                                                <i class="fas {{ $topic->is_active ? 'fa-toggle-off' : 'fa-toggle-on' }}"></i>
                                                {{ $topic->is_active ? 'Deactivate' : 'Activate' }}
                                            </button>
                                        </form>
                                        @endcan
                                        @can('delete discussion topics')
                                        <form action="{{ route('knowledge-sharing.discussions.topics.destroy', $topic) }}" method="POST" class="inline"
                                              onsubmit="return confirm('Delete this topic? This cannot be undone.')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn-action-delete">
                                                <i class="fas fa-trash"></i> Delete
                                            </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="6" class="px-4 py-12 text-center">
                                    <i class="fas fa-lightbulb text-4xl text-gray-300 mb-3 block"></i>
                                    <p class="text-gray-500 mb-4">No topics found.</p>
                                    @can('create discussion topics')
                                    <a href="{{ route('knowledge-sharing.discussions.topics.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 First Topic
                                    </a>
                                    @endcan
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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

        </div>
    </div>

</x-app-layout>
