<?php

namespace App\Http\Controllers\API\V1\Informacia\Frontend;

use App\Http\Controllers\API\BaseController;
use App\Repositories\Eloquent\Criteria\AnyRolePouzivatelaCriteria;
use App\Repositories\Eloquent\Criteria\ZverejneneCriteria;
use App\Repositories\InformaciaRepositoryContract;
use Dingo\Api\Http\Response;
use Exception;

/**
 * Class ZiskatInformaciuAction
 */
final class ZiskatInformaciuAction extends BaseController
{
    private InformaciaRepositoryContract $informaciaRepository;

    public function __construct(InformaciaRepositoryContract $informaciaRepository)
    {
        $this->informaciaRepository = $informaciaRepository;
    }

    public function __invoke(int $id): Response
    {
        try {
            $informacia = $this->informaciaRepository
                ->pushCriteria(ZverejneneCriteria::class)
                ->pushCriteria(AnyRolePouzivatelaCriteria::class)
                ->with('vytvoril')
                ->findOrFail($id);

            return $this->response->item($informacia);
        } catch (Exception $e) {
            $this->response->errorInternal('Záznam informácie sa nepodarilo získať.');
        }
    }
}
