<x-app-layout>

    <x-module-hero
        title="{{ \Illuminate\Support\Str::limit($question->title, 60) }}"
        description="{{ $question->category ? $question->category->name : 'Knowledge Base Question' }}"
        icon="fa-circle-question">

        <x-slot name="backButton">
            <a href="{{ route('knowledge-sharing.knowledge-base.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 Questions
            </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 knowledge base questions')
        <x-slot name="actions">
            <a href="{{ route('knowledge-sharing.knowledge-base.edit', $question) }}"
               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
            </a>
        </x-slot>
        @endcan

    </x-module-hero>

    <div class="p-6">

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

            {{-- Main Content --}}
            <div class="lg:col-span-3 space-y-6">

                {{-- Status Badges --}}
                <div class="flex flex-wrap gap-2">
                    @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'];
                        $dc = ['beginner'=>'bg-green-100 text-green-800','intermediate'=>'bg-yellow-100 text-yellow-800','advanced'=>'bg-red-100 text-red-800'];
                    @endphp
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {{ $sc[$question->status] ?? 'bg-gray-100 text-gray-700' }}">
                        {{ ucfirst(str_replace('_', ' ', $question->status)) }}
                    </span>
                    @if($question->is_featured)
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-800">
                            <i class="fas fa-star mr-1"></i> Featured
                        </span>
                    @endif
                    @if($question->difficulty)
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {{ $dc[$question->difficulty] ?? 'bg-gray-100 text-gray-700' }}">
                            {{ ucfirst($question->difficulty) }}
                        </span>
                    @endif
                </div>

                {{-- Stats --}}
                <div class="grid grid-cols-3 gap-4">
                    <div class="bg-white rounded-lg border border-gray-200 p-4 text-center">
                        <p class="text-2xl font-bold text-gray-800">{{ number_format($question->view_count) }}</p>
                        <p class="text-xs text-gray-500 mt-1">Views</p>
                    </div>
                    <div class="bg-white rounded-lg border border-gray-200 p-4 text-center">
                        <p class="text-2xl font-bold text-gray-800">{{ $question->answers()->count() }}</p>
                        <p class="text-xs text-gray-500 mt-1">Answers</p>
                    </div>
                    <div class="bg-white rounded-lg border border-gray-200 p-4 text-center">
                        <p class="text-2xl font-bold text-gray-800">{{ $question->helpful_count }}</p>
                        <p class="text-xs text-gray-500 mt-1">Helpful Votes</p>
                    </div>
                </div>

                {{-- Question Content --}}
                <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">
                        <div>
                            <h3 class="text-base font-semibold text-gray-800">{{ $question->title }}</h3>
                            <p class="text-xs text-gray-500 mt-0.5">
                                by {{ $question->user->name }}
                                @if($question->category)
                                    &bull; <i class="fas fa-folder mr-1"></i>{{ $question->category->name }}
                                @endif
                                &bull; {{ $question->created_at->format('M d, Y') }}
                            </p>
                        </div>
                        @if($question->is_active ?? true)
                            <a href="{{ route('public.knowledge-base.show', $question->slug) }}" target="_blank"
                               class="text-xs text-blue-600 hover:underline">
                                <i class="fas fa-external-link-alt mr-1"></i> View on Site
                            </a>
                        @endif
                    </div>
                    <div class="p-6">
                        <div class="prose max-w-none text-gray-700 text-sm">
                            {!! $question->content !!}
                        </div>
                        @if($question->tags)
                            <div class="flex flex-wrap gap-2 mt-4 pt-4 border-t border-gray-100">
                                @foreach(is_array($question->tags) ? $question->tags : explode(',', $question->tags) as $tag)
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-700">
                                        {{ trim($tag) }}
                                    </span>
                                @endforeach
                            </div>
                        @endif
                    </div>
                    <div class="px-6 py-3 border-t border-gray-200 bg-gray-50 flex items-center gap-2">
                        @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> Publish
                            </button>
                        </form>
                        @endif
                        @endcan
                        @can('archive knowledge base questions')
                        @if($question->status === 'published')
                        <form action="{{ route('knowledge-sharing.knowledge-base.archive', $question) }}" method="POST" class="inline">
                            @csrf
                            <button type="submit" class="btn-action-reject">
                                <i class="fas fa-archive"></i> Archive
                            </button>
                        </form>
                        @endif
                        @endcan
                        @can('feature knowledge base questions')
                        <form action="{{ route('knowledge-sharing.knowledge-base.toggle-featured', $question) }}" method="POST" class="inline">
                            @csrf
                            <button type="submit"
                                    class="px-3 py-1.5 text-xs border rounded-lg transition-colors {{ $question->is_featured ? 'border-yellow-500 text-yellow-600 hover:bg-yellow-50' : 'border-gray-300 text-gray-600 hover:bg-gray-50' }}">
                                <i class="fas fa-star mr-1"></i>{{ $question->is_featured ? 'Unfeature' : 'Feature' }}
                            </button>
                        </form>
                        @endcan
                    </div>
                </div>

                {{-- Answers --}}
                <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-lightbulb text-blue-600 mr-2"></i>
                            Answers ({{ $question->answers->count() }})
                        </h3>
                        @can('answer knowledge base questions')
                        @if($question->is_published)
                        <a href="{{ route('knowledge-sharing.knowledge-base.answers.create', $question) }}"
                           class="inline-flex items-center gap-2 px-3 py-1.5 bg-blue-600 text-white text-xs font-semibold rounded-lg hover:bg-blue-700 transition">
                            <i class="fas fa-lightbulb"></i> Add Answer
                        </a>
                        @endif
                        @endcan
                    </div>
                    @if($question->answers->isNotEmpty())
                        <div class="divide-y divide-gray-100">
                            @foreach($question->answers as $answer)
                                <div class="p-4 {{ $answer->is_accepted ? 'bg-green-50' : '' }}">
                                    <div class="flex items-start gap-3">
                                        <div class="w-8 h-8 rounded-full bg-gray-200 flex items-center justify-center flex-shrink-0 text-xs font-semibold text-gray-600">
                                            {{ strtoupper(substr($answer->user->name, 0, 1)) }}
                                        </div>
                                        <div class="flex-1 min-w-0">
                                            <div class="flex items-center justify-between mb-1">
                                                <div class="flex items-center gap-2">
                                                    <span class="text-sm font-medium text-gray-800">{{ $answer->user->name }}</span>
                                                    @if($answer->is_accepted)
                                                        <span class="px-2 py-0.5 bg-green-100 text-green-800 text-xs rounded">
                                                            <i class="fas fa-check mr-1"></i>Accepted
                                                        </span>
                                                    @endif
                                                    @if(!$answer->is_published)
                                                        <span class="px-2 py-0.5 bg-gray-100 text-gray-600 text-xs rounded">Draft</span>
                                                    @endif
                                                </div>
                                                <div class="flex items-center gap-2">
                                                    <span class="text-xs text-gray-400">{{ $answer->created_at->diffForHumans() }}</span>
                                                    {{-- Accept (question owner or manager) --}}
                                                    @if(($question->user_id === auth()->id() || auth()->user()->hasAnyRole(['Manager','Super Manager'])) && !$answer->is_accepted)
                                                        <form action="{{ route('knowledge-sharing.knowledge-base.answers.accept', [$question, $answer]) }}"
                                                              method="POST" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-xs text-green-600 hover:text-green-800 font-medium">
                                                                <i class="fas fa-check-circle mr-1"></i>Accept
                                                            </button>
                                                        </form>
                                                    @endif
                                                    {{-- Edit --}}
                                                    @if($answer->user_id === auth()->id() || auth()->user()->can('edit knowledge base answers'))
                                                        <a href="{{ route('knowledge-sharing.knowledge-base.answers.edit', [$question, $answer]) }}"
                                                           class="text-xs text-amber-600 hover:underline">
                                                            <i class="fas fa-edit"></i>
                                                        </a>
                                                    @endif
                                                    {{-- Delete --}}
                                                    @if($answer->user_id === auth()->id() || auth()->user()->can('delete knowledge base answers'))
                                                        <form action="{{ route('knowledge-sharing.knowledge-base.answers.destroy', [$question, $answer]) }}"
                                                              method="POST" class="inline"
                                                              onsubmit="return confirm('Delete this answer?')">
                                                            @csrf
                                                            @method('DELETE')
                                                            <button type="submit" class="text-xs text-red-500 hover:text-red-700">
                                                                <i class="fas fa-trash"></i>
                                                            </button>
                                                        </form>
                                                    @endif
                                                </div>
                                            </div>
                                            <div class="text-sm text-gray-600">
                                                {{ \Illuminate\Support\Str::limit(strip_tags($answer->content), 300) }}
                                            </div>
                                            <div class="flex items-center gap-3 mt-2 text-xs text-gray-400">
                                                <span><i class="fas fa-thumbs-up mr-1 text-blue-400"></i>{{ $answer->helpful_count }}</span>
                                                <span><i class="fas fa-percentage mr-1 text-green-400"></i>{{ $answer->helpful_percentage }}% helpful</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @else
                        <div class="p-10 text-center">
                            <i class="fas fa-lightbulb text-4xl text-gray-300 mb-3 block"></i>
                            <p class="text-gray-500 mb-3">No answers yet.</p>
                            @can('answer knowledge base questions')
                            @if($question->is_published)
                            <a href="{{ route('knowledge-sharing.knowledge-base.answers.create', $question) }}"
                               class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white text-sm font-semibold rounded-lg hover:bg-blue-700 transition">
                                <i class="fas fa-lightbulb"></i> Be the first to answer
                            </a>
                            @endif
                            @endcan
                        </div>
                    @endif
                </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('answer knowledge base questions')
                        @if($question->is_published)
                        <a href="{{ route('knowledge-sharing.knowledge-base.answers.create', $question) }}"
                           class="flex items-center gap-3 px-3 py-2 rounded-lg hover:bg-blue-50 transition-colors text-sm text-blue-700 font-medium">
                            <i class="fas fa-lightbulb text-blue-500 w-4"></i> Add Answer
                        </a>
                        @endif
                        @endcan
                        @can('edit knowledge base questions')
                        <a href="{{ route('knowledge-sharing.knowledge-base.edit', $question) }}"
                           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 Question
                        </a>
                        @endcan
                        @can('delete knowledge base questions')
                        <form action="{{ route('knowledge-sharing.knowledge-base.destroy', $question) }}" method="POST"
                              onsubmit="return confirm('Delete this question?')">
                            @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 Question
                            </button>
                        </form>
                        @endcan
                    </div>
                </div>

                {{-- Metadata --}}
                <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> Metadata
                    </h3>
                    <dl class="space-y-3 text-sm">
                        <div>
                            <dt class="text-gray-500">ID</dt>
                            <dd class="font-mono text-gray-800 mt-0.5">#{{ $question->id }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Slug</dt>
                            <dd class="font-mono text-gray-800 text-xs mt-0.5 break-all">{{ $question->slug }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Category</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $question->category?->name ?? '—' }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Author</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $question->user->name }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Created</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $question->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">{{ $question->updated_at->format('M d, Y H:i') }}</dd>
                        </div>
                        @if($question->published_at)
                        <div>
                            <dt class="text-gray-500">Published</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $question->published_at->format('M d, Y') }}</dd>
                        </div>
                        @endif
                    </dl>
                </div>

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

</x-app-layout>
