<x-app-layout>

    <x-module-hero
        title="{{ $topic->name }}"
        description="{{ $topic->description ?? 'Discussion topic details' }}"
        icon="{{ $topic->icon ?? 'fa-lightbulb' }}">

        <x-slot name="backButton">
            <a href="{{ route('knowledge-sharing.discussions.topics.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 Topics
            </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 discussion topics')
        <x-slot name="actions">
            <a href="{{ route('knowledge-sharing.discussions.topics.edit', $topic) }}"
               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 Topic
            </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">

            {{-- 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-comments text-green-600 mr-2"></i>
                            Recent Discussions
                        </h3>
                        <span class="text-sm text-gray-500">{{ $topic->active_discussions_count ?? 0 }} total</span>
                    </div>

                    @if($topic->discussions->isNotEmpty())
                        <div class="divide-y divide-gray-100">
                            @foreach($topic->discussions as $discussion)
                                <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">
                                            <a href="{{ route('knowledge-sharing.discussions.show', $discussion) }}"
                                               class="font-medium text-gray-900 hover:text-green-600 transition-colors line-clamp-1">
                                                {{ $discussion->title }}
                                            </a>
                                            <p class="text-xs text-gray-500 mt-1">
                                                by {{ $discussion->user->name }} &bull; {{ $discussion->created_at->diffForHumans() }}
                                            </p>
                                        </div>
                                        <div class="flex items-center gap-3 text-xs text-gray-500 flex-shrink-0">
                                            <span><i class="fas fa-eye mr-1"></i>{{ number_format($discussion->view_count) }}</span>
                                            <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-800' : 'bg-gray-100 text-gray-700' }}">
                                                {{ ucfirst($discussion->status) }}
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @else
                        <div class="p-10 text-center">
                            <i class="fas fa-comment-slash text-4xl text-gray-300 mb-3 block"></i>
                            <p class="text-gray-500">No discussions in this topic yet.</p>
                        </div>
                    @endif
                </div>
            </div>

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

                {{-- Topic Details --}}
                <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> Topic Details
                    </h3>
                    <dl class="space-y-3 text-sm">
                        <div>
                            <dt class="text-gray-500">Status</dt>
                            <dd class="mt-0.5">
                                @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
                            </dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Slug</dt>
                            <dd class="font-mono text-gray-800 text-xs mt-0.5">{{ $topic->slug }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Icon</dt>
                            <dd class="text-gray-800 mt-0.5">
                                <i class="fas {{ $topic->icon ?? 'fa-folder' }} mr-1"></i> {{ $topic->icon ?? 'fa-folder' }}
                            </dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Color</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $topic->color ? ucfirst($topic->color) : 'Default' }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Sort Order</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $topic->sort_order }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Created</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $topic->created_at->format('M d, Y H:i') }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Updated</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $topic->updated_at->format('M d, Y H:i') }}</dd>
                        </div>
                    </dl>
                </div>

                {{-- SEO Info --}}
                @if($topic->meta_title || $topic->meta_description || $topic->meta_keywords)
                <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-search text-blue-500 mr-2"></i> SEO Settings
                    </h3>
                    <dl class="space-y-3 text-sm">
                        @if($topic->meta_title)
                        <div>
                            <dt class="text-gray-500">Meta Title</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $topic->meta_title }}</dd>
                        </div>
                        @endif
                        @if($topic->meta_description)
                        <div>
                            <dt class="text-gray-500">Meta Description</dt>
                            <dd class="text-gray-600 mt-0.5">{{ $topic->meta_description }}</dd>
                        </div>
                        @endif
                        @if($topic->meta_keywords)
                        <div>
                            <dt class="text-gray-500">Meta Keywords</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $topic->meta_keywords }}</dd>
                        </div>
                        @endif
                    </dl>
                </div>
                @endif

                {{-- Actions --}}
                @can('edit discussion 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-bolt text-green-600 mr-2"></i> Actions
                    </h3>
                    <div class="space-y-2">
                        <a href="{{ route('knowledge-sharing.discussions.topics.edit', $topic) }}"
                           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 Topic
                        </a>
                        <form action="{{ route('knowledge-sharing.discussions.topics.toggle-active', $topic) }}" 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 {{ $topic->is_active ? 'fa-toggle-off text-red-500' : 'fa-toggle-on text-green-500' }} w-4"></i>
                                {{ $topic->is_active ? 'Deactivate' : 'Activate' }} Topic
                            </button>
                        </form>
                        @can('delete discussion topics')
                        <form action="{{ route('knowledge-sharing.discussions.topics.destroy', $topic) }}" method="POST"
                              onsubmit="return confirm('Delete this topic? This cannot be undone.')">
                            @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 Topic
                            </button>
                        </form>
                        @endcan
                    </div>
                </div>
                @endcan

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

</x-app-layout>
