@extends('themes.saijana.layouts.app')

@section('title', $thread->title.' - Forum')

@section('content')
<div class="max-w-4xl mx-auto px-6 py-16">
    <nav class="mb-6 text-sm">
        <a href="{{ route('forum.index') }}" class="text-slate-600 hover:text-slate-900">Forum</a>
        <span class="text-slate-400 mx-2">/</span>
        <a href="{{ route('forum.category', $category->slug) }}" class="text-slate-600 hover:text-slate-900">{{ $category->name }}</a>
    </nav>

    <div class="bg-white rounded-2xl border border-slate-200 overflow-hidden mb-8">
        <div class="p-6 border-b border-slate-100">
            <div class="flex items-center gap-2 mb-2">
                @if($thread->is_pinned)
                    <span class="px-2 py-0.5 bg-amber-100 text-amber-800 text-xs font-medium rounded">Épinglé</span>
                @endif
                @if($thread->is_locked)
                    <span class="px-2 py-0.5 bg-slate-100 text-slate-600 text-xs font-medium rounded">Verrouillé</span>
                @endif
            </div>
            <h1 class="text-2xl font-bold text-slate-900">{{ $thread->title }}</h1>
            <p class="text-slate-500 mt-2">par <strong>{{ $thread->author_name }}</strong> · {{ $thread->created_at->format('d/m/Y H:i') }}</p>
        </div>
        <div class="p-6 prose prose-slate max-w-none">
            {!! \App\Support\SafeHtml::richText($thread->content) !!}
        </div>
    </div>

    @foreach($thread->replies as $reply)
        <div class="bg-white rounded-xl border border-slate-200 p-6 mb-4">
            <p class="text-slate-500 text-sm mb-2"><strong>{{ $reply->author_name }}</strong> · {{ $reply->created_at->format('d/m/Y H:i') }}</p>
            <div class="prose prose-slate max-w-none">
                {!! \App\Support\SafeHtml::richText($reply->content) !!}
            </div>
        </div>
    @endforeach

    @if(!$thread->is_locked)
        <div class="bg-white rounded-2xl border border-slate-200 p-6 mt-8">
            <h2 class="text-xl font-semibold text-slate-900 mb-4">Répondre</h2>
            <form method="POST" action="{{ route('forum.reply', [$category->slug, $thread->slug]) }}" class="space-y-4">
                @csrf
                <div class="grid sm:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-slate-700 mb-1">Nom *</label>
                        <input type="text" name="author_name" value="{{ old('author_name', auth()->user()?->name) }}" required class="w-full border border-slate-200 rounded-xl px-4 py-2.5">
                        @error('author_name')<p class="text-red-600 text-sm mt-1">{{ $message }}</p>@enderror
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-slate-700 mb-1">Email *</label>
                        <input type="email" name="author_email" value="{{ old('author_email', auth()->user()?->email) }}" required class="w-full border border-slate-200 rounded-xl px-4 py-2.5">
                        @error('author_email')<p class="text-red-600 text-sm mt-1">{{ $message }}</p>@enderror
                    </div>
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700 mb-1">Message *</label>
                    <textarea name="content" rows="4" required class="w-full border border-slate-200 rounded-xl px-4 py-2.5">{{ old('content') }}</textarea>
                    @error('content')<p class="text-red-600 text-sm mt-1">{{ $message }}</p>@enderror
                </div>
                <button type="submit" class="px-6 py-2.5 bg-slate-900 text-white rounded-xl font-medium hover:bg-slate-800 transition">Publier</button>
            </form>
        </div>
    @endif
</div>
@endsection
