<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ForumCategory;
use App\Models\ForumReply;
use App\Models\ForumThread;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ForumController extends Controller
{
    public function index()
    {
        $categories = ForumCategory::orderBy('order')->orderBy('name')->get();
        $threadsCount = ForumThread::count();
        $repliesCount = ForumReply::count();
        $pendingThreads = ForumThread::where('is_approved', false)->count();
        $pendingReplies = ForumReply::where('is_approved', false)->count();

        return view('admin.forum.index', compact('categories', 'threadsCount', 'repliesCount', 'pendingThreads', 'pendingReplies'));
    }

    public function categories()
    {
        $categories = ForumCategory::orderBy('order')->orderBy('name')->get();

        return view('admin.forum.categories', compact('categories'));
    }

    public function storeCategory(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string|max:1000',
        ]);
        ForumCategory::create([
            'name' => $request->name,
            'slug' => Str::slug($request->name),
            'description' => $request->description,
            'moderation_enabled' => $request->boolean('moderation_enabled'),
        ]);

        return back()->with('success', 'Catégorie créée.');
    }

    public function updateCategory(Request $request, ForumCategory $forumCategory)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string|max:1000',
            'order' => 'nullable|integer|min:0',
        ]);
        $forumCategory->update([
            'name' => $request->name,
            'slug' => Str::slug($request->name),
            'description' => $request->description,
            'order' => $request->order ?? 0,
            'moderation_enabled' => $request->boolean('moderation_enabled'),
        ]);

        return back()->with('success', 'Catégorie mise à jour.');
    }

    public function destroyCategory(ForumCategory $forumCategory)
    {
        $forumCategory->delete();

        return back()->with('success', 'Catégorie supprimée.');
    }

    public function threads()
    {
        $threads = ForumThread::with('category')->latest()->paginate(30);

        return view('admin.forum.threads', compact('threads'));
    }

    public function thread(ForumThread $forumThread)
    {
        $forumThread->load(['replies', 'category']);

        return view('admin.forum.thread', compact('forumThread'));
    }

    public function updateThread(Request $request, ForumThread $forumThread)
    {
        $request->validate([
            'is_pinned' => 'boolean',
            'is_locked' => 'boolean',
        ]);
        $forumThread->update([
            'is_pinned' => $request->boolean('is_pinned'),
            'is_locked' => $request->boolean('is_locked'),
        ]);

        return back()->with('success', 'Sujet mis à jour.');
    }

    public function destroyThread(ForumThread $forumThread)
    {
        $forumThread->delete();

        return redirect()->route('admin.forum.threads')->with('success', 'Sujet supprimé.');
    }

    public function destroyReply(ForumReply $forumReply)
    {
        $thread = $forumReply->thread;
        $forumReply->delete();
        $thread->syncReplyStats();

        return back()->with('success', 'Réponse supprimée.');
    }

    public function approveThread(ForumThread $forumThread)
    {
        $forumThread->update(['is_approved' => true]);

        return back()->with('success', 'Sujet validé : il est visible sur le site public.');
    }

    public function approveReply(ForumReply $forumReply)
    {
        $forumReply->update(['is_approved' => true]);
        $forumReply->thread->syncReplyStats();

        return back()->with('success', 'Réponse validée.');
    }
}
