<?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;
use Prettus\Repository\Exceptions\RepositoryException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * Class ZiskatInformaciuAction
 *
 * @package App\Http\Controllers\API\V1\Informacia\Frontend
 */
final class ZiskatInformaciuAction extends BaseController
{
    /**
     * @var InformaciaRepositoryContract
     */
    private InformaciaRepositoryContract $informaciaRepository;

    /**
     * @param InformaciaRepositoryContract $informaciaRepository
     */
    public function __construct(InformaciaRepositoryContract $informaciaRepository)
    {
        $this->informaciaRepository = $informaciaRepository;
    }

    /**
     * @return Response|void
     * @throws RepositoryException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    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ť.');
        }
    }
}
