<x-app-layout>

    <x-module-hero
        title="Reply to Discussion"
        description="{{ Str::limit($discussion->title, 60) }}"
        icon="fa-reply">

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

        {{-- Discussion 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-teal-100 flex items-center justify-center flex-shrink-0">
                    <i class="fas fa-comments text-teal-600"></i>
                </div>
                <div class="flex-1 min-w-0">
                    <p class="text-xs text-gray-500 mb-1">Replying to discussion</p>
                    <h3 class="text-base font-semibold text-gray-800">{{ $discussion->title }}</h3>
                    <p class="text-sm text-gray-500 mt-1">
                        by {{ $discussion->user->name }}
                        @if($discussion->topic)
                            &bull; <i class="fas {{ $discussion->topic->icon ?? 'fa-folder' }} mr-1"></i>{{ $discussion->topic->name }}
                        @endif
                        &bull; {{ $discussion->created_at->format('M d, Y') }}
                    </p>
                </div>
            </div>
        </div>

        {{-- Reply 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-reply text-teal-600"></i> Write Your Reply
                </h2>
            </div>
            <div class="p-6">
                <form action="{{ route('knowledge-sharing.discussions.replies.store', $discussion) }}" method="POST">
                    @csrf

                    {{-- Parent Reply (optional threading) --}}
                    @if($parentReplies->isNotEmpty())
                        <div class="mb-5">
                            <label for="parent_id" class="block text-sm font-medium text-gray-700 mb-1">
                                Reply to (optional — leave blank for top-level reply)
                            </label>
                            <select name="parent_id" id="parent_id"
                                    class="w-full px-4 py-2.5 border border-gray-300 rounded-lg bg-white text-gray-700 focus:ring-2 focus:ring-teal-500 focus:border-transparent text-sm">
                                <option value="">— Top-level reply —</option>
                                @foreach($parentReplies as $parent)
                                    <option value="{{ $parent->id }}" {{ old('parent_id') == $parent->id ? 'selected' : '' }}>
                                        {{ $parent->user->name }}: {{ Str::limit(strip_tags($parent->content), 80) }}
                                    </option>
                                @endforeach
                            </select>
                            @error('parent_id')
                                <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                            @enderror
                        </div>
                    @endif

                    {{-- Content --}}
                    <div class="mb-5">
                        <label for="content" class="block text-sm font-medium text-gray-700 mb-1">
                            Reply Content <span class="text-red-500">*</span>
                        </label>
                        <textarea
                            name="content"
                            id="content"
                            rows="8"
                            maxlength="5000"
                            placeholder="Write your reply here. Be clear, helpful, and respectful. Minimum 10 characters."
                            class="w-full px-4 py-3 border border-gray-300 rounded-lg bg-white text-gray-700 focus:ring-2 focus:ring-teal-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 10 characters, maximum 5000.</p>
                            @enderror
                            <span id="charCount" class="text-xs text-gray-400 ml-auto">0 / 5000</span>
                        </div>
                    </div>

                    {{-- Actions --}}
                    <div class="flex items-center justify-between pt-4 border-t border-gray-100">
                        <a href="{{ route('knowledge-sharing.discussions.show', $discussion) }}"
                           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-teal-600 text-white text-sm font-semibold rounded-lg hover:bg-teal-700 transition shadow-sm">
                            <i class="fas fa-paper-plane mr-2"></i>Post Reply
                        </button>
                    </div>
                </form>
            </div>
        </div>

    </div>

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

</x-app-layout>
