<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Category\CategoryMoveRequest;
use App\Http\Requests\Category\CategoryReorderRequest;
use App\Http\Requests\Category\CategoryStoreRequest;
use App\Http\Requests\Category\CategoryUpdateRequest;
use App\Services\CategoryService;
use Illuminate\Http\Request;

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

    public function index(Request $request)
    {
        return ApiResponse::handle(fn () => $this->service->index($request->all()), __('message.category.fetched'));
    }

    public function show(int $id)
    {
        return ApiResponse::handle(fn () => $this->service->show($id), __('message.category.fetched'));
    }

    public function store(CategoryStoreRequest $request)
    {
        return ApiResponse::handle(fn () => $this->service->store($request->validated()), __('message.category.created'));
    }

    public function update(int $id, CategoryUpdateRequest $request)
    {
        return ApiResponse::handle(fn () => $this->service->update($id, $request->validated()), __('message.category.updated'));
    }

    public function destroy(int $id)
    {
        return ApiResponse::handle(fn () => $this->service->destroy($id), __('message.category.deleted'));
    }

    public function toggle(int $id)
    {
        return ApiResponse::handle(fn () => $this->service->toggle($id), __('message.category.toggled'));
    }

    public function move(int $id, CategoryMoveRequest $request)
    {
        return ApiResponse::handle(fn () => $this->service->move($id, $request->validated()), __('message.category.moved'));
    }

    public function reorder(CategoryReorderRequest $request)
    {
        return ApiResponse::handle(fn () => $this->service->reorder($request->validated()), __('message.category.reordered'));
    }

    public function tree(Request $request)
    {
        return ApiResponse::handle(fn () => $this->service->tree($request->all()), __('message.category.tree_fetched'));
    }
}
