<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\CategoryRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Category;
use App\Services\Admin\Master\CategoryService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class CategoryController extends Controller
{
    public function __construct(
        private CategoryService $service
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/master/category/index', [
            'categories' => $this->service->paginated(...$request->validatedWithDefaults()),
        ]);
    }

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

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Kategori berhasil ditambahkan.']);

        return back();
    }

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

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Kategori berhasil diperbarui.']);

        return back();
    }

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

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Kategori berhasil dihapus.']);

        return back();
    }
}
