<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\CategoryRequest;
use App\Models\Category;
use App\Services\Master\CategoryService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class CategoryController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly CategoryService $categoryService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);

        return Inertia::render('admin/master/categories/Index', [
            'categories' => $this->categoryService->paginateForIndex($tableQuery),
            'filters' => $this->dataTableFilters($tableQuery),
        ]);
    }

    public function store(CategoryRequest $request): RedirectResponse
    {
        $this->categoryService->create($request->validated());

        $this->flashCreated('Kategori');

        return redirect()->route('admin.master.categories.index');
    }

    public function update(CategoryRequest $request, Category $category): RedirectResponse
    {
        $this->categoryService->update($category, $request->validated());

        $this->flashUpdated('Kategori');

        return redirect()->route('admin.master.categories.index');
    }

    public function destroy(Category $category): RedirectResponse
    {
        $this->categoryService->delete($category);

        $this->flashDeleted('Kategori');

        return redirect()->route('admin.master.categories.index');
    }
}
