<x-app-layout>

    <x-module-hero
        title="My Discussions"
        description="All discussions you have created"
        icon="fa-comments">

        <x-slot name="backButton">
            <a href="{{ route('contributor.dashboard') }}"
               class="inline-flex items-center gap-2 text-white/80 hover:text-white text-sm transition-colors">
                <i class="fas fa-arrow-left"></i> Back to Dashboard
            </a>
        </x-slot>

        <x-slot name="tabs">
            <a href="{{ route('contributor.dashboard') }}" class="module-hero-tab">
                <i class="fas fa-gauge-high"></i> Dashboard
            </a>
            <a href="{{ route('contributor.discussions') }}"
               class="module-hero-tab {{ request()->routeIs('contributor.discussions') ? 'active-tab' : '' }}">
                <i class="fas fa-comments"></i> My Discussions
            </a>
            <a href="{{ route('contributor.replies') }}" class="module-hero-tab">
                <i class="fas fa-reply"></i> My Replies
            </a>

            @can('view discussion topics')
            <a href="{{ route('knowledge-sharing.discussions.topics.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.topics.*') ? 'active-tab' : '' }}">
                <i class="fas fa-lightbulb"></i> Topics
            </a>
            @endcan

            @can('view discussions')
            <a href="{{ route('knowledge-sharing.discussions.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.index') ? 'active-tab' : '' }}">
                <i class="fas fa-message"></i> All Discussions
            </a>
            @endcan

            @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.*') ? 'active-tab' : '' }}">
                <i class="fas fa-book-open"></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.*')) ? 'active-tab' : '' }}">
                <i class="fas fa-circle-question"></i> Knowledge Base
            </a>
            @endcan
        </x-slot>

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

    </x-module-hero>

    <div class="p-6">

        <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 your discussions…"
                               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="topic_id" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                        <option value="">All Topics</option>
                        @foreach($topics as $topic)
                            <option value="{{ $topic->id }}" {{ request('topic_id') == $topic->id ? 'selected' : '' }}>{{ $topic->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>
                    </select>
                    <select name="sort" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500">
                        <option value="latest" {{ request('sort', 'latest') === 'latest' ? 'selected' : '' }}>Latest</option>
                        <option value="popular" {{ request('sort') === 'popular' ? 'selected' : '' }}>Most Viewed</option>
                        <option value="most_replies" {{ request('sort') === 'most_replies' ? 'selected' : '' }}>Most Replies</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">Discussion</th>
                            <th class="th-col-2 px-4 py-3 text-left">Topic</th>
                            <th class="th-col-3 px-4 py-3 text-center">Status</th>
                            <th class="th-col-4 px-4 py-3 text-center">Stats</th>
                            <th class="th-col-5 px-4 py-3 text-center">Created</th>
                            <th class="th-col-6 px-4 py-3 text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($discussions as $discussion)
                            <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                                <td class="px-4 py-3">
                                    <a href="{{ route('knowledge-sharing.discussions.show', $discussion) }}"
                                       class="font-medium text-gray-900 hover:text-green-600 transition-colors">
                                        {{ \Illuminate\Support\Str::limit($discussion->title, 55) }}
                                    </a>
                                </td>
                                <td class="px-4 py-3">
                                    @if($discussion->topic)
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                            {{ $discussion->topic->name }}
                                        </span>
                                    @else
                                        <span class="text-gray-400 text-xs">No topic</span>
                                    @endif
                                </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'];
                                    @endphp
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ $sc[$discussion->status] ?? 'bg-gray-100 text-gray-700' }}">
                                        {{ ucfirst($discussion->status) }}
                                    </span>
                                </td>
                                <td class="px-4 py-3 text-center">
                                    <div class="flex items-center justify-center gap-3 text-xs text-gray-500">
                                        <span><i class="fas fa-eye mr-1"></i>{{ $discussion->view_count }}</span>
                                        <span><i class="fas fa-reply mr-1"></i>{{ $discussion->active_replies_count ?? 0 }}</span>
                                    </div>
                                </td>
                                <td class="px-4 py-3 text-center text-sm text-gray-500">
                                    {{ $discussion->created_at->format('M d, Y') }}
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex items-center justify-center gap-1">
                                        <a href="{{ route('public.discussions.show', $discussion->slug) }}" class="btn-action-view" title="View Public">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{{ route('knowledge-sharing.discussions.edit', $discussion) }}" class="btn-action-edit">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <form action="{{ route('knowledge-sharing.discussions.destroy', $discussion) }}" method="POST" class="inline"
                                              onsubmit="return confirm('Delete this discussion?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn-action-delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    </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-comments text-4xl text-gray-300 mb-3 block"></i>
                                    <p class="text-gray-500 mb-4">No discussions found.</p>
                                    <a href="{{ route('knowledge-sharing.discussions.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 Your First Discussion
                                    </a>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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

        </div>
    </div>

</x-app-layout>
