<x-app-layout>

    <x-module-hero
        title="Contributor Dashboard"
        description="Manage your discussions and contributions to the community"
        icon="fa-pen-nib">

        <x-slot name="tabs">
            {{-- Contributor-specific tabs --}}
            <a href="{{ route('contributor.dashboard') }}"
               class="module-hero-tab {{ request()->routeIs('contributor.dashboard') ? 'active-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 {{ request()->routeIs('contributor.replies') ? 'active-tab' : '' }}">
                <i class="fas fa-reply"></i> My Replies
            </a>

            {{-- Knowledge Sharing module tabs (permission-gated) --}}
            @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') || (request()->routeIs('knowledge-sharing.discussions.*') && !request()->routeIs('knowledge-sharing.discussions.topics.*')) ? '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">

        @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

        {{-- Stats Cards --}}
        <div class="grid grid-cols-2 md:grid-cols-5 gap-4 mb-6">
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-blue-100 rounded-lg flex items-center justify-center flex-shrink-0">
                        <i class="fas fa-comments text-blue-600"></i>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">Total Discussions</p>
                        <p class="text-xl font-bold text-gray-800">{{ $statistics['total_discussions'] }}</p>
                    </div>
                </div>
            </div>
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-green-100 rounded-lg flex items-center justify-center flex-shrink-0">
                        <i class="fas fa-check-circle text-green-600"></i>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">Published</p>
                        <p class="text-xl font-bold text-gray-800">{{ $statistics['published_discussions'] }}</p>
                    </div>
                </div>
            </div>
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-yellow-100 rounded-lg flex items-center justify-center flex-shrink-0">
                        <i class="fas fa-edit text-yellow-600"></i>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">Drafts</p>
                        <p class="text-xl font-bold text-gray-800">{{ $statistics['draft_discussions'] }}</p>
                    </div>
                </div>
            </div>
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-purple-100 rounded-lg flex items-center justify-center flex-shrink-0">
                        <i class="fas fa-eye text-purple-600"></i>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">Total Views</p>
                        <p class="text-xl font-bold text-gray-800">{{ number_format($statistics['total_views']) }}</p>
                    </div>
                </div>
            </div>
            <div class="bg-white rounded-lg border border-gray-200 p-4">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 bg-pink-100 rounded-lg flex items-center justify-center flex-shrink-0">
                        <i class="fas fa-reply text-pink-600"></i>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">Replies Made</p>
                        <p class="text-xl font-bold text-gray-800">{{ $statistics['total_replies'] }}</p>
                    </div>
                </div>
            </div>
        </div>

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

            {{-- Recent Discussions --}}
            <div class="lg:col-span-2">
                <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-history text-green-600 mr-2"></i> Your Recent Discussions
                        </h3>
                        <a href="{{ route('contributor.discussions') }}" class="text-sm text-green-600 hover:underline">
                            View All <i class="fas fa-arrow-right ml-1"></i>
                        </a>
                    </div>
                    <div class="divide-y divide-gray-100">
                        @forelse($recentDiscussions as $discussion)
                            <div class="px-6 py-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 mb-1">
                                            <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium
                                                {{ $discussion->status === 'published' ? 'bg-green-100 text-green-700' : 'bg-yellow-100 text-yellow-700' }}">
                                                {{ ucfirst($discussion->status) }}
                                            </span>
                                            @if($discussion->topic)
                                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-700">
                                                    {{ $discussion->topic->name }}
                                                </span>
                                            @endif
                                        </div>
                                        <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, 60) }}
                                        </a>
                                        <div class="flex items-center gap-4 mt-1 text-xs text-gray-400">
                                            <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>
                                            <span><i class="fas fa-clock mr-1"></i>{{ $discussion->created_at->diffForHumans() }}</span>
                                        </div>
                                    </div>
                                    <a href="{{ route('knowledge-sharing.discussions.edit', $discussion) }}"
                                       class="btn-action-edit flex-shrink-0">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                </div>
                            </div>
                        @empty
                            <div class="px-6 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">You haven't created any discussions yet.</p>
                                @can('create discussions')
                                <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>
                                @endcan
                            </div>
                        @endforelse
                    </div>
                </div>
            </div>

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

                {{-- Quick Actions --}}
                <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> Quick Actions
                    </h3>
                    <div class="space-y-2">
                        @can('create discussions')
                        <a href="{{ route('knowledge-sharing.discussions.create') }}"
                           class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition-colors">
                            <div class="w-9 h-9 bg-blue-100 rounded-lg flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-plus text-blue-600 text-sm"></i>
                            </div>
                            <span class="text-sm text-gray-700">Create Discussion</span>
                        </a>
                        @endcan
                        <a href="{{ route('public.discussions.index') }}"
                           class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition-colors">
                            <div class="w-9 h-9 bg-green-100 rounded-lg flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-globe text-green-600 text-sm"></i>
                            </div>
                            <span class="text-sm text-gray-700">Browse Public Discussions</span>
                        </a>
                        <a href="{{ route('contributor.replies') }}"
                           class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition-colors">
                            <div class="w-9 h-9 bg-purple-100 rounded-lg flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-reply text-purple-600 text-sm"></i>
                            </div>
                            <span class="text-sm text-gray-700">View Your Replies</span>
                        </a>
                        @can('view knowledge base questions')
                        <a href="{{ route('knowledge-sharing.knowledge-base.index') }}"
                           class="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition-colors">
                            <div class="w-9 h-9 bg-teal-100 rounded-lg flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-circle-question text-teal-600 text-sm"></i>
                            </div>
                            <span class="text-sm text-gray-700">Knowledge Base</span>
                        </a>
                        @endcan
                    </div>
                </div>

                {{-- Popular Topics --}}
                <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-tags text-green-600 mr-2"></i> Popular Topics
                    </h3>
                    <div class="space-y-2">
                        @forelse($topics as $topic)
                            <a href="{{ route('public.discussions.topic', $topic->slug) }}"
                               class="flex items-center justify-between p-2 rounded-lg hover:bg-gray-50 transition-colors">
                                <span class="text-sm text-gray-700">{{ $topic->name }}</span>
                                <span class="text-xs text-gray-400">{{ $topic->discussions_count }}</span>
                            </a>
                        @empty
                            <p class="text-sm text-gray-400">No topics available.</p>
                        @endforelse
                    </div>
                </div>

                {{-- Popular Discussions --}}
                <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-fire text-orange-500 mr-2"></i> Popular Discussions
                    </h3>
                    <div class="space-y-3">
                        @forelse($popularDiscussions as $discussion)
                            <a href="{{ route('public.discussions.show', $discussion->slug) }}"
                               class="block p-3 rounded-lg hover:bg-gray-50 transition-colors">
                                <p class="text-sm font-medium text-gray-800 line-clamp-2">{{ $discussion->title }}</p>
                                <div class="flex items-center gap-2 mt-1 text-xs text-gray-400">
                                    <span><i class="fas fa-eye mr-1"></i>{{ $discussion->view_count }}</span>
                                    <span>by {{ $discussion->user->name }}</span>
                                </div>
                            </a>
                        @empty
                            <p class="text-sm text-gray-400">No popular discussions yet.</p>
                        @endforelse
                    </div>
                </div>

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

</x-app-layout>
