<?php

namespace App\Http\Controllers\Api\Registries;

use App\Http\Controllers\Controller;
use App\Models\Registries\Category;
use App\Models\Registries\Registry;
use App\Transformers\Registries\CategoryTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CategoriesController extends Controller {
    protected Category $model;

    public function __construct(Category $model) {
        $this->model = $model;
    }

    public function index(Request $request): JsonResponse {
        $registry = Registry::byUuid($request->get('registry'))->first();
        $parent = Category::byUuid($request->get('parent'))->first();
        $query = $this->model->query()->where(['registry_id' => $registry->id ?? 0, 'parent_id' => $parent->id ?? 0]);
        $paginator = $query->paginate(config('app.pagination_limit'));
        return fractal($paginator, new CategoryTransformer())->respond();
    }

    public function show(Request $request, $id): JsonResponse {
        $model = $this->model->byUuid($id)->firstOrFail();
        return fractal($model, new CategoryTransformer())->respond();
    }


    public function store(Request $request): void {
    }


    public function update(Request $request, $uuid): void	{
    }

    public function destroy(Request $request, $uuid): JsonResponse {
        $model = $this->model->byUuid($uuid)->firstOrFail();
        $model->delete();
        return response()->json(null, 204);
    }
}
