<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ChatCategory;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Str;

class ChatCategoryController extends Controller
{
    public function index(): View
    {
        $categories = ChatCategory::withCount('rooms')
            ->orderBy('order')
            ->get();

        return view('admin.chat-categories.index', compact('categories'));
    }

    public function create(): View
    {
        return view('admin.chat-categories.create');
    }

    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100|unique:chat_categories,name',
            'description' => 'nullable|string|max:500',
            'icon' => 'nullable|string|max:50',
            'color' => 'nullable|string|max:20',
            'order' => 'nullable|integer|min:0|max:999',
            'is_active' => 'nullable|boolean',
        ]);

        $validated['slug'] = Str::slug($validated['name']);
        $validated['is_active'] = $request->has('is_active');
        $validated['order'] = $validated['order'] ?? 0;

        ChatCategory::create($validated);

        return redirect()
            ->route('admin.chat-categories.index')
            ->with('success', 'Kategori oluşturuldu.');
    }

    public function edit(ChatCategory $category): View
    {
        return view('admin.chat-categories.edit', compact('category'));
    }

    public function update(Request $request, ChatCategory $category): RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100|unique:chat_categories,name,' . $category->id,
            'description' => 'nullable|string|max:500',
            'icon' => 'nullable|string|max:50',
            'color' => 'nullable|string|max:20',
            'order' => 'nullable|integer|min:0|max:999',
            'is_active' => 'nullable|boolean',
        ]);

        $validated['slug'] = Str::slug($validated['name']);
        $validated['is_active'] = $request->has('is_active');
        $validated['order'] = $validated['order'] ?? 0;

        $category->update($validated);

        return redirect()
            ->route('admin.chat-categories.index')
            ->with('success', 'Kategori güncellendi.');
    }

    public function destroy(ChatCategory $category): RedirectResponse
    {
        if ($category->rooms()->count() > 0) {
            return redirect()
                ->route('admin.chat-categories.index')
                ->with('error', 'Bu kategoriye ait odalar var. Önce odaları silin veya başka kategoriye taşıyın.');
        }

        $category->delete();

        return redirect()
            ->route('admin.chat-categories.index')
            ->with('success', 'Kategori silindi.');
    }

    public function toggleActive(ChatCategory $category): RedirectResponse
    {
        $category->update([
            'is_active' => !$category->is_active
        ]);

        return redirect()
            ->route('admin.chat-categories.index')
            ->with('success', 'Kategori durumu güncellendi.');
    }
}
