@extends('admin.layout')

@section('title', $forumThread->title)
@section('header', 'Sujet Forum')

@section('content')
<div class="max-w-4xl">
    @if(!$forumThread->is_approved)
        <div class="mb-4 p-4 bg-amber-50 border border-amber-200 rounded-xl flex flex-wrap items-center justify-between gap-3">
            <p class="text-sm text-amber-900">Ce sujet n’est pas encore visible sur le site public.</p>
            <form method="POST" action="{{ route('admin.forum.thread.approve', $forumThread) }}">
                @csrf
                <button type="submit" class="px-4 py-2 bg-amber-600 text-white rounded-lg text-sm font-medium hover:bg-amber-700">Valider le sujet</button>
            </form>
        </div>
    @endif
    <div class="bg-white rounded-xl border border-slate-200 p-6 mb-6">
        <h1 class="text-xl font-bold text-slate-900">{{ $forumThread->title }}</h1>
        <p class="text-slate-500 mt-2">par {{ $forumThread->author_name }} · {{ $forumThread->created_at->format('d/m/Y H:i') }}</p>
        <div class="mt-4 prose prose-slate max-w-none">
            {!! \App\Support\SafeHtml::richText($forumThread->content) !!}
        </div>
    </div>

    <form method="POST" action="{{ route('admin.forum.thread.update', $forumThread) }}" class="flex gap-4 mb-8">
        @csrf
        @method('PUT')
        <label class="flex items-center gap-2">
            <input type="checkbox" name="is_pinned" value="1" {{ $forumThread->is_pinned ? 'checked' : '' }} class="rounded border-slate-300">
            <span class="text-sm">Épingler</span>
        </label>
        <label class="flex items-center gap-2">
            <input type="checkbox" name="is_locked" value="1" {{ $forumThread->is_locked ? 'checked' : '' }} class="rounded border-slate-300">
            <span class="text-sm">Verrouiller</span>
        </label>
        <button type="submit" class="px-4 py-2 bg-primary-500 text-white rounded-lg text-sm hover:bg-primary-600">Enregistrer</button>
    </form>

    <h2 class="text-lg font-semibold text-slate-900 mb-4">Réponses ({{ $forumThread->replies->count() }})</h2>
    <div class="space-y-4">
        @foreach($forumThread->replies as $reply)
            <div class="bg-white rounded-xl border {{ $reply->is_approved ? 'border-slate-200' : 'border-amber-300 ring-1 ring-amber-100' }} p-4 flex justify-between items-start gap-4">
                <div class="min-w-0 flex-1">
                    @if(!$reply->is_approved)
                        <p class="text-xs font-medium text-amber-800 mb-2">Réponse en attente de validation</p>
                    @endif
                    <p class="font-medium text-slate-900">{{ $reply->author_name }}</p>
                    <p class="text-slate-500 text-sm">{{ $reply->created_at->format('d/m/Y H:i') }}</p>
                    <div class="mt-2 prose prose-slate text-sm max-w-none">{!! \App\Support\SafeHtml::richText($reply->content) !!}</div>
                </div>
                <div class="flex flex-col gap-2 items-end shrink-0">
                    @if(!$reply->is_approved)
                        <form method="POST" action="{{ route('admin.forum.reply.approve', $reply) }}">
                            @csrf
                            <button type="submit" class="text-sm text-amber-700 font-medium hover:underline">Valider</button>
                        </form>
                    @endif
                    <form method="POST" action="{{ route('admin.forum.reply.destroy', $reply) }}" onsubmit="return confirm('Supprimer cette réponse ?')">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="text-red-600 hover:text-red-700 text-sm">Supprimer</button>
                    </form>
                </div>
            </div>
        @endforeach
    </div>

    <div class="mt-8 flex gap-3">
        <a href="{{ route('admin.forum.threads') }}" class="text-slate-600 hover:text-slate-900">← Sujets</a>
        <form method="POST" action="{{ route('admin.forum.thread.destroy', $forumThread) }}" onsubmit="return confirm('Supprimer ce sujet et toutes les réponses ?')" class="inline">
            @csrf
            @method('DELETE')
            <button type="submit" class="text-red-600 hover:text-red-700">Supprimer le sujet</button>
        </form>
    </div>
</div>
@endsection
