<x-app-layout>

    <x-module-hero
        title="{{ \Illuminate\Support\Str::limit($discussion->title, 60) }}"
        description="Discussion details and replies"
        icon="fa-comments">

        <x-slot name="backButton">
            <a href="{{ route('knowledge-sharing.discussions.index') }}"
               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 Discussions
            </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-slot name="actions">
            @can('edit discussions')
            <a href="{{ route('knowledge-sharing.discussions.edit', $discussion) }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-white text-[#1a4731] text-sm font-semibold rounded-lg hover:bg-green-50 transition-colors">
                <i class="fas fa-edit"></i> Edit
            </a>
            @endcan
        </x-slot>

    </x-module-hero>

    <div class="p-6">

        <div class="grid grid-cols-1 lg:grid-cols-4 gap-6">

            {{-- Main Content --}}
            <div class="lg:col-span-3 space-y-6">

                {{-- Status Badges --}}
                <div class="flex flex-wrap gap-2">
                    @php
                        $sc = ['published'=>'bg-green-100 text-green-800','draft'=>'bg-gray-100 text-gray-700','pending_review'=>'bg-yellow-100 text-yellow-800','archived'=>'bg-red-100 text-red-800'];
                    @endphp
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {{ $sc[$discussion->status] ?? 'bg-gray-100 text-gray-700' }}">
                        {{ ucfirst(str_replace('_', ' ', $discussion->status)) }}
                    </span>
                    @if($discussion->is_featured)
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-800">
                            <i class="fas fa-star mr-1"></i> Featured
                        </span>
                    @endif
                    @if($discussion->is_pinned)
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-purple-100 text-purple-800">
                            <i class="fas fa-thumbtack mr-1"></i> Pinned
                        </span>
                    @endif
                    @if(!$discussion->is_active)
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-800">
                            <i class="fas fa-ban mr-1"></i> Inactive
                        </span>
                    @endif
                </div>

                {{-- Discussion Content --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
                        <div>
                            <h3 class="text-base font-semibold text-gray-800">{{ $discussion->title }}</h3>
                            <p class="text-xs text-gray-500 mt-0.5">
                                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 class="flex items-center gap-3 text-xs text-gray-500">
                            <span><i class="fas fa-eye mr-1"></i>{{ number_format($discussion->view_count) }}</span>
                            <span><i class="fas fa-comment mr-1"></i>{{ $discussion->active_replies_count ?? $discussion->activeReplies->count() }}</span>
                        </div>
                    </div>
                    <div class="p-6">
                        <div class="prose max-w-none text-gray-700 text-sm">
                            {!! $discussion->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-2">
                            @can('feature discussions')
                            <form action="{{ route('knowledge-sharing.discussions.toggle-featured', $discussion) }}" method="POST" class="inline">
                                @csrf
                                <button type="submit"
                                        class="px-3 py-1.5 text-xs border rounded-lg transition-colors {{ $discussion->is_featured ? 'border-yellow-500 text-yellow-600 hover:bg-yellow-50' : 'border-gray-300 text-gray-600 hover:bg-gray-50' }}">
                                    <i class="fas fa-star mr-1"></i>{{ $discussion->is_featured ? 'Unfeature' : 'Feature' }}
                                </button>
                            </form>
                            @endcan
                            @can('pin discussions')
                            <form action="{{ route('knowledge-sharing.discussions.toggle-pinned', $discussion) }}" method="POST" class="inline">
                                @csrf
                                <button type="submit"
                                        class="px-3 py-1.5 text-xs border rounded-lg transition-colors {{ $discussion->is_pinned ? 'border-purple-500 text-purple-600 hover:bg-purple-50' : 'border-gray-300 text-gray-600 hover:bg-gray-50' }}">
                                    <i class="fas fa-thumbtack mr-1"></i>{{ $discussion->is_pinned ? 'Unpin' : 'Pin' }}
                                </button>
                            </form>
                            @endcan
                        </div>
                        <a href="{{ route('public.discussions.show', $discussion->slug) }}" target="_blank"
                           class="text-xs text-blue-600 hover:underline">
                            <i class="fas fa-external-link-alt mr-1"></i> View on Site
                        </a>
                    </div>
                </div>

                {{-- Replies --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
                        <h3 class="text-base font-semibold text-gray-800">
                            <i class="fas fa-comments text-green-600 mr-2"></i>
                            Replies ({{ $discussion->activeReplies->count() }})
                        </h3>
                        @if(auth()->user()->can('create discussion replies') || auth()->user()->can('create discussions'))
                        <a href="{{ route('knowledge-sharing.discussions.replies.create', $discussion) }}"
                           class="inline-flex items-center gap-2 px-3 py-1.5 bg-teal-600 text-white text-xs font-semibold rounded-lg hover:bg-teal-700 transition">
                            <i class="fas fa-reply"></i> Add Reply
                        </a>
                        @endif
                    </div>
                    @if($discussion->activeReplies->isNotEmpty())
                        <div class="divide-y divide-gray-100">
                            @foreach($discussion->activeReplies->take(15) as $reply)
                                <div class="p-4 {{ $reply->is_accepted_answer ? 'bg-green-50' : '' }}">
                                    <div class="flex items-start gap-3">
                                        <div class="w-8 h-8 rounded-full bg-gray-200 flex items-center justify-center flex-shrink-0 text-xs font-semibold text-gray-600">
                                            {{ strtoupper(substr($reply->user->name, 0, 1)) }}
                                        </div>
                                        <div class="flex-1 min-w-0">
                                            <div class="flex items-center justify-between mb-1">
                                                <div class="flex items-center gap-2">
                                                    <span class="text-sm font-medium text-gray-800">{{ $reply->user->name }}</span>
                                                    @if($reply->is_accepted_answer)
                                                        <span class="px-2 py-0.5 bg-green-100 text-green-800 text-xs rounded">
                                                            <i class="fas fa-check mr-1"></i>Accepted
                                                        </span>
                                                    @endif
                                                    @if(!$reply->is_active)
                                                        <span class="px-2 py-0.5 bg-red-100 text-red-800 text-xs rounded">Hidden</span>
                                                    @endif
                                                </div>
                                                <div class="flex items-center gap-2">
                                                    <span class="text-xs text-gray-400">{{ $reply->created_at->diffForHumans() }}</span>
                                                    @if($reply->user_id === auth()->id() || auth()->user()->can('edit discussion replies'))
                                                        <a href="{{ route('knowledge-sharing.discussions.replies.edit', [$discussion, $reply]) }}"
                                                           class="text-xs text-amber-600 hover:underline">
                                                            <i class="fas fa-edit"></i>
                                                        </a>
                                                    @endif
                                                    @if($reply->user_id === auth()->id() || auth()->user()->can('delete discussion replies'))
                                                        <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="text-xs text-red-500 hover:text-red-700">
                                                                <i class="fas fa-trash"></i>
                                                            </button>
                                                        </form>
                                                    @endif
                                                </div>
                                            </div>
                                            <div class="text-sm text-gray-600">
                                                {{ \Illuminate\Support\Str::limit(strip_tags($reply->content), 300) }}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                        @if($discussion->activeReplies->count() > 15)
                            <div class="p-4 text-center border-t border-gray-100">
                                <span class="text-sm text-gray-500">And {{ $discussion->activeReplies->count() - 15 }} more replies…</span>
                            </div>
                        @endif
                    @else
                        <div class="p-10 text-center">
                            <i class="fas fa-comment-slash text-4xl text-gray-300 mb-3 block"></i>
                            <p class="text-gray-500 mb-3">No replies yet.</p>
                            @can('create discussion replies')
                            <a href="{{ route('knowledge-sharing.discussions.replies.create', $discussion) }}"
                               class="inline-flex items-center gap-2 px-4 py-2 bg-teal-600 text-white text-sm font-semibold rounded-lg hover:bg-teal-700 transition">
                                <i class="fas fa-reply"></i> Be the first to reply
                            </a>
                            @endcan
                        </div>
                    @endif
                </div>

            </div>

            {{-- Sidebar --}}
            <div class="space-y-6">

                {{-- Quick Actions --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-4">
                        <i class="fas fa-bolt text-green-600 mr-2"></i> Quick Actions
                    </h3>
                    <div class="space-y-2">
                        @can('edit discussions')
                        <a href="{{ route('knowledge-sharing.discussions.edit', $discussion) }}"
                           class="flex items-center gap-3 px-3 py-2 rounded-lg hover:bg-gray-50 transition-colors text-sm text-gray-700">
                            <i class="fas fa-edit text-amber-500 w-4"></i> Edit Discussion
                        </a>
                        @endcan
                        @can('delete discussions')
                        <form action="{{ route('knowledge-sharing.discussions.destroy', $discussion) }}" method="POST"
                              onsubmit="return confirm('Delete this discussion?')">
                            @csrf
                            @method('DELETE')
                            <button type="submit"
                                    class="flex items-center gap-3 w-full px-3 py-2 rounded-lg hover:bg-red-50 text-red-600 transition-colors text-sm text-left">
                                <i class="fas fa-trash w-4"></i> Delete Discussion
                            </button>
                        </form>
                        @endcan
                    </div>
                </div>

                {{-- Metadata --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5">
                    <h3 class="text-base font-semibold text-gray-800 mb-4">
                        <i class="fas fa-info-circle text-blue-500 mr-2"></i> Metadata
                    </h3>
                    <dl class="space-y-3 text-sm">
                        <div>
                            <dt class="text-gray-500">ID</dt>
                            <dd class="font-mono text-gray-800 mt-0.5">#{{ $discussion->id }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Slug</dt>
                            <dd class="font-mono text-gray-800 text-xs mt-0.5 break-all">{{ $discussion->slug }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Created</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $discussion->created_at->format('M d, Y H:i') }}</dd>
                        </div>
                        <div>
                            <dt class="text-gray-500">Updated</dt>
                            <dd class="text-gray-800 mt-0.5">{{ $discussion->updated_at->format('M d, Y H:i') }}</dd>
                        </div>
                    </dl>
                </div>

            </div>
        </div>
    </div>

</x-app-layout>
