<?php

namespace App\Http\Controllers\Api\Registries;

use App\Http\Controllers\Controller;
use App\Models\Pages\Page;
use App\Models\Registries\Registry;
use App\Transformers\Publications\PublicationTransformer;
use App\Transformers\Registries\RegistryTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RegistriesController extends Controller {
    protected Registry $model;

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

    public function index(Request $request): JsonResponse {
        $query = $this->model->query();
        if ($page = Page::byUuid($request->get('page'))->first()) $query->where(['page_id' => $page->id]);
        $paginator = $query->paginate(config('app.pagination_limit'));
        return fractal($paginator, new RegistryTransformer())->respond();
    }

    public function show(Request $request, $id): JsonResponse {
        $model = $this->model->byUuid($id)->firstOrFail();
        return fractal($model, new PublicationTransformer())->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);
    }
}
