<?php

namespace App\Http\Controllers\Api\Registries;

use App\Http\Controllers\Controller;
use App\Models\Objects\NirObject;
use App\Models\Registries\Entry;
use App\Transformers\Objects\ObjectTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OperationsController extends Controller {
    protected NirObject $model;

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

    public function index(Request $request): JsonResponse {
        $query = ($entry = Entry::byUuid($request->get('entry'))->first()) ? $entry->operations() : $this->model->query();
        $paginator = $query->paginate(config('app.pagination_limit'));
        return fractal($paginator, new ObjectTransformer())->respond();
    }

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