<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreKbCategoryRequest;
use App\Http\Requests\Admin\UpdateKbCategoryRequest;
use App\Models\KnowledgeBaseCategory;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class KnowledgeBaseCategoryController extends Controller
{
    public function index(): Response
    {
        $categories = KnowledgeBaseCategory::query()
            ->withCount('articles')
            ->with('parent:id,name')
            ->orderBy('sort_order')
            ->orderBy('name')
            ->get();

        return Inertia::render('Admin/KnowledgeBase/Categories/Index', [
            'categories' => $categories,
        ]);
    }

    public function create(): Response
    {
        $parentCategories = KnowledgeBaseCategory::query()
            ->root()
            ->orderBy('sort_order')
            ->orderBy('name')
            ->get(['id', 'name']);

        return Inertia::render('Admin/KnowledgeBase/Categories/Create', [
            'parentCategories' => $parentCategories,
        ]);
    }

    public function store(StoreKbCategoryRequest $request): RedirectResponse
    {
        KnowledgeBaseCategory::query()->create([
            'name' => $request->validated('name'),
            'slug' => $request->validated('slug'),
            'description' => $request->validated('description'),
            'icon' => $request->validated('icon'),
            'parent_id' => $request->validated('parent_id'),
            'sort_order' => $request->validated('sort_order', 0),
            'is_visible' => $request->boolean('is_visible', true),
        ]);

        return redirect()
            ->route('admin.kb.categories.index')
            ->with('success', 'Category created successfully.');
    }

    public function edit(KnowledgeBaseCategory $category): Response
    {
        $parentCategories = KnowledgeBaseCategory::query()
            ->root()
            ->where('id', '!=', $category->id)
            ->orderBy('sort_order')
            ->orderBy('name')
            ->get(['id', 'name']);

        return Inertia::render('Admin/KnowledgeBase/Categories/Edit', [
            'category' => $category,
            'parentCategories' => $parentCategories,
        ]);
    }

    public function update(UpdateKbCategoryRequest $request, KnowledgeBaseCategory $category): RedirectResponse
    {
        $category->update([
            'name' => $request->validated('name'),
            'slug' => $request->validated('slug'),
            'description' => $request->validated('description'),
            'icon' => $request->validated('icon'),
            'parent_id' => $request->validated('parent_id'),
            'sort_order' => $request->validated('sort_order', 0),
            'is_visible' => $request->boolean('is_visible', true),
        ]);

        return redirect()
            ->route('admin.kb.categories.index')
            ->with('success', 'Category updated successfully.');
    }

    public function destroy(KnowledgeBaseCategory $category): RedirectResponse
    {
        if ($category->articles()->exists()) {
            return redirect()
                ->back()
                ->with('error', 'Cannot delete a category that has articles. Move or delete the articles first.');
        }

        $category->delete();

        return redirect()
            ->route('admin.kb.categories.index')
            ->with('success', 'Category deleted successfully.');
    }
}
