<x-app-layout>

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

        {{-- Reply 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-teal-100 flex items-center justify-center text-sm font-semibold text-teal-700">
                        {{ strtoupper(substr($reply->user->name, 0, 1)) }}
                    </div>
                    <div>
                        <p class="text-sm font-semibold text-gray-800">{{ $reply->user->name }}</p>
                        <p class="text-xs text-gray-500">{{ $reply->created_at->format('M d, Y H:i') }}</p>
                    </div>
                </div>
                <div class="flex items-center gap-2">
                    @if($reply->is_accepted_answer)
                        <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 {{ $reply->is_active ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }} text-xs font-semibold rounded-full">
                        {{ $reply->is_active ? 'Active' : 'Inactive' }}
                    </span>
                </div>
            </div>
            <div class="p-6">
                <div class="prose max-w-none text-gray-700 text-sm">
                    {!! $reply->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-3 text-xs text-gray-500">
                    <span><i class="fas fa-thumbs-up mr-1"></i>{{ $reply->like_count }} likes</span>
                    <span><i class="fas fa-level-down-alt mr-1"></i>Depth {{ $reply->depth }}</span>
                    @if($reply->parent_id)
                        <span><i class="fas fa-reply mr-1"></i>Nested reply</span>
                    @endif
                </div>
                <div class="flex items-center gap-2">
                    @if($reply->user_id === auth()->id() || auth()->user()->hasAnyRole(['Manager', 'Super Manager']))
                        <a href="{{ route('knowledge-sharing.discussions.replies.edit', [$discussion, $reply]) }}"
                           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.discussions.replies.destroy', [$discussion, $reply]) }}"
                              method="POST" class="inline"
                              onsubmit="return confirm('Delete this reply?')">
                            @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
                </div>
            </div>
        </div>

        {{-- Child Replies --}}
        @if($childReplies->isNotEmpty())
            <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">
                    <h3 class="text-sm font-semibold text-gray-800">
                        <i class="fas fa-reply-all text-teal-600 mr-2"></i>
                        Nested Replies ({{ $childReplies->count() }})
                    </h3>
                </div>
                <div class="divide-y divide-gray-100">
                    @foreach($childReplies as $child)
                        <div class="p-4">
                            <div class="flex items-start gap-3">
                                <div class="w-7 h-7 rounded-full bg-gray-200 flex items-center justify-center text-xs font-semibold text-gray-600">
                                    {{ strtoupper(substr($child->user->name, 0, 1)) }}
                                </div>
                                <div class="flex-1">
                                    <div class="flex items-center justify-between mb-1">
                                        <span class="text-sm font-medium text-gray-800">{{ $child->user->name }}</span>
                                        <span class="text-xs text-gray-400">{{ $child->created_at->diffForHumans() }}</span>
                                    </div>
                                    <p class="text-sm text-gray-600">{{ Str::limit(strip_tags($child->content), 200) }}</p>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
        @endif

    </div>

</x-app-layout>
