<x-app-layout>

    <x-module-hero
        title="View Answer"
        description="{{ Str::limit($question->title, 60) }}"
        icon="fa-lightbulb">

        <x-slot name="backButton">
            <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}"
               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 Question
            </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>

    </x-module-hero>

    <div class="p-6 max-w-4xl mx-auto">

        {{-- Answer Card --}}
        <div class="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden mb-6">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div class="w-9 h-9 rounded-full bg-blue-100 flex items-center justify-center text-sm font-semibold text-blue-700">
                        {{ strtoupper(substr($answer->user->name, 0, 1)) }}
                    </div>
                    <div>
                        <p class="text-sm font-semibold text-gray-800">{{ $answer->user->name }}</p>
                        <p class="text-xs text-gray-500">{{ $answer->created_at->format('M d, Y H:i') }}</p>
                    </div>
                </div>
                <div class="flex items-center gap-2">
                    @if($answer->is_accepted)
                        <span class="px-2.5 py-1 bg-green-100 text-green-700 text-xs font-semibold rounded-full">
                            <i class="fas fa-check-circle mr-1"></i>Accepted Answer
                        </span>
                    @endif
                    <span class="px-2.5 py-1 {{ $answer->is_published ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }} text-xs font-semibold rounded-full">
                        {{ $answer->is_published ? 'Published' : 'Draft' }}
                    </span>
                </div>
            </div>
            <div class="p-6">
                <div class="prose max-w-none text-gray-700 text-sm">
                    {!! $answer->content !!}
                </div>
            </div>
            <div class="px-6 py-3 border-t border-gray-200 bg-gray-50 flex items-center justify-between">
                <div class="flex items-center gap-4 text-xs text-gray-500">
                    <span><i class="fas fa-thumbs-up mr-1 text-blue-400"></i>{{ $answer->helpful_count }} helpful</span>
                    <span><i class="fas fa-thumbs-down mr-1 text-red-400"></i>{{ $answer->not_helpful_count }} not helpful</span>
                    <span><i class="fas fa-percentage mr-1 text-green-400"></i>{{ $answer->helpful_percentage }}% helpful</span>
                    <span><i class="fas fa-sort-numeric-up mr-1"></i>Sort order: {{ $answer->sort_order }}</span>
                </div>
                <div class="flex items-center gap-2">
                    @if($answer->user_id === auth()->id() || auth()->user()->hasAnyRole(['Manager', 'Super Manager']))
                        <a href="{{ route('knowledge-sharing.knowledge-base.answers.edit', [$question, $answer]) }}"
                           class="px-3 py-1.5 text-xs border border-amber-300 text-amber-600 rounded-lg hover:bg-amber-50 transition">
                            <i class="fas fa-edit mr-1"></i>Edit
                        </a>
                        <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="px-3 py-1.5 text-xs border border-red-300 text-red-600 rounded-lg hover:bg-red-50 transition">
                                <i class="fas fa-trash mr-1"></i>Delete
                            </button>
                        </form>
                    @endif
                    @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="px-3 py-1.5 text-xs border border-green-300 text-green-600 rounded-lg hover:bg-green-50 transition">
                                <i class="fas fa-check-circle mr-1"></i>Accept
                            </button>
                        </form>
                    @endif
                </div>
            </div>
        </div>

        {{-- Question Context --}}
        <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-3 flex items-center gap-2">
                <i class="fas fa-circle-question text-blue-600"></i>Question Context
            </h3>
            <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}"
               class="text-blue-600 hover:underline font-medium text-sm">{{ $question->title }}</a>
            <p class="text-xs text-gray-500 mt-1">
                by {{ $question->user->name }}
                @if($question->category)
                    · <i class="fas fa-folder mr-1"></i>{{ $question->category->name }}
                @endif
                · {{ $question->created_at->format('M d, Y') }}
            </p>
        </div>

    </div>

</x-app-layout>
