<x-app-layout>

    <x-module-hero
        title="Submit 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">

        {{-- Question Context --}}
        <div class="bg-white rounded-xl border border-gray-200 shadow-sm p-5 mb-6">
            <div class="flex items-start gap-3">
                <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center flex-shrink-0">
                    <i class="fas fa-circle-question text-blue-600"></i>
                </div>
                <div class="flex-1 min-w-0">
                    <p class="text-xs text-gray-500 mb-1">Answering question</p>
                    <h3 class="text-base font-semibold text-gray-800">{{ $question->title }}</h3>
                    <div class="flex items-center gap-3 mt-1 text-xs text-gray-500">
                        <span>by {{ $question->user->name }}</span>
                        @if($question->category)
                            <span>&bull;</span>
                            <span><i class="fas fa-folder mr-1"></i>{{ $question->category->name }}</span>
                        @endif
                        @if($question->difficulty)
                            <span>&bull;</span>
                            <span class="capitalize">{{ $question->difficulty }}</span>
                        @endif
                    </div>
                </div>
            </div>
        </div>

        {{-- Existing Answers Preview --}}
        @if($existingAnswers->isNotEmpty())
            <div class="bg-blue-50 border border-blue-200 rounded-xl p-4 mb-6">
                <h4 class="text-sm font-semibold text-blue-800 mb-2 flex items-center gap-2">
                    <i class="fas fa-info-circle"></i>
                    {{ $existingAnswers->count() }} existing answer(s) — make sure yours adds new value
                </h4>
                <div class="space-y-2">
                    @foreach($existingAnswers->take(3) as $existing)
                        <div class="bg-white rounded-lg p-3 border border-blue-100">
                            <div class="flex items-center gap-2 mb-1">
                                <span class="text-xs font-medium text-gray-700">{{ $existing->user->name }}</span>
                                @if($existing->is_accepted)
                                    <span class="px-1.5 py-0.5 bg-green-100 text-green-700 text-xs rounded">Accepted</span>
                                @endif
                            </div>
                            <p class="text-xs text-gray-600 line-clamp-2">{{ Str::limit(strip_tags($existing->content), 150) }}</p>
                        </div>
                    @endforeach
                </div>
            </div>
        @endif

        {{-- Answer Form --}}
        <div class="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                <h2 class="text-base font-semibold text-gray-800 flex items-center gap-2">
                    <i class="fas fa-lightbulb text-blue-600"></i> Write Your Answer
                </h2>
            </div>
            <div class="p-6">
                <form action="{{ route('knowledge-sharing.knowledge-base.answers.store', $question) }}" method="POST">
                    @csrf

                    {{-- Content --}}
                    <div class="mb-5">
                        <label for="content" class="block text-sm font-medium text-gray-700 mb-1">
                            Answer Content <span class="text-red-500">*</span>
                        </label>
                        <textarea
                            name="content"
                            id="content"
                            rows="10"
                            maxlength="10000"
                            placeholder="Write a detailed, helpful answer. Include examples, references, or step-by-step instructions where applicable. Minimum 20 characters."
                            class="w-full px-4 py-3 border border-gray-300 rounded-lg bg-white text-gray-700 focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm resize-y"
                            oninput="updateCharCount(this)"
                            required>{{ old('content') }}</textarea>
                        <div class="flex items-center justify-between mt-1">
                            @error('content')
                                <p class="text-xs text-red-600">{{ $message }}</p>
                            @else
                                <p class="text-xs text-gray-400">Minimum 20 characters, maximum 10,000.</p>
                            @enderror
                            <span id="charCount" class="text-xs text-gray-400 ml-auto">0 / 10000</span>
                        </div>
                    </div>

                    {{-- Actions --}}
                    <div class="flex items-center justify-between pt-4 border-t border-gray-100">
                        <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}"
                           class="px-5 py-2.5 border border-gray-300 text-gray-700 text-sm font-medium rounded-lg hover:bg-gray-50 transition">
                            <i class="fas fa-times mr-2"></i>Cancel
                        </a>
                        <button type="submit"
                                class="px-6 py-2.5 bg-blue-600 text-white text-sm font-semibold rounded-lg hover:bg-blue-700 transition shadow-sm">
                            <i class="fas fa-paper-plane mr-2"></i>Submit Answer
                        </button>
                    </div>
                </form>
            </div>
        </div>

    </div>

    @push('scripts')
    <script>
        function updateCharCount(el) {
            document.getElementById('charCount').textContent = el.value.length + ' / 10000';
        }
        const ta = document.getElementById('content');
        if (ta) updateCharCount(ta);
    </script>
    @endpush

</x-app-layout>
