<x-app-layout>

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

        <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">

        {{-- Reply Meta --}}
        <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-amber-100 flex items-center justify-center flex-shrink-0">
                    <i class="fas fa-reply text-amber-600"></i>
                </div>
                <div class="flex-1 min-w-0">
                    <p class="text-xs text-gray-500 mb-1">Editing reply in</p>
                    <h3 class="text-base font-semibold text-gray-800">{{ $discussion->title }}</h3>
                    <div class="flex items-center gap-3 mt-1 text-xs text-gray-500">
                        <span>by {{ $reply->user->name }}</span>
                        <span>&bull;</span>
                        <span>Posted {{ $reply->created_at->format('M d, Y H:i') }}</span>
                        @if($reply->updated_at->gt($reply->created_at))
                            <span>&bull;</span>
                            <span class="text-amber-600">Last edited {{ $reply->updated_at->diffForHumans() }}</span>
                        @endif
                    </div>
                </div>
            </div>
        </div>

        {{-- Edit 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-edit text-amber-600"></i> Edit Reply Content
                </h2>
            </div>
            <div class="p-6">
                <form action="{{ route('knowledge-sharing.discussions.replies.update', [$discussion, $reply]) }}" method="POST">
                    @csrf
                    @method('PUT')

                    {{-- 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="Edit your reply content..."
                            class="w-full px-4 py-3 border border-gray-300 rounded-lg bg-white text-gray-700 focus:ring-2 focus:ring-amber-500 focus:border-transparent text-sm resize-y"
                            oninput="updateCharCount(this)"
                            required>{{ old('content', $reply->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">
                        <div class="flex items-center gap-3">
                            <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>
                            @if($reply->user_id === auth()->id() || auth()->user()->hasAnyRole(['Manager', 'Super Manager']))
                                <form action="{{ route('knowledge-sharing.discussions.replies.destroy', [$discussion, $reply]) }}"
                                      method="POST" class="inline"
                                      onsubmit="return confirm('Delete this reply permanently?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit"
                                            class="px-5 py-2.5 border border-red-300 text-red-600 text-sm font-medium rounded-lg hover:bg-red-50 transition">
                                        <i class="fas fa-trash mr-2"></i>Delete
                                    </button>
                                </form>
                            @endif
                        </div>
                        <button type="submit"
                                class="px-6 py-2.5 bg-amber-600 text-white text-sm font-semibold rounded-lg hover:bg-amber-700 transition shadow-sm">
                            <i class="fas fa-save mr-2"></i>Save Changes
                        </button>
                    </div>
                </form>
            </div>
        </div>

    </div>

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

</x-app-layout>
