<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Http\View\Composers;

use Illuminate\Contracts\View\View;
use Prettus\Repository\Criteria\RequestCriteria;
use Seka\Portal\Modul\Statistiky\Repositories\ZaznamRepositoryContract;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\DruhSubjektuZaznamuEnum;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Presenters\ZaznamDataTablePresenter;

/**
 * Class PekZoznamPoctuKontrolPodlaKategorieVozidlaDataTableComposer
 *
 * @package Seka\Portal\Modul\Statistiky\Http\View\Composers
 */
final class PekZoznamPoctuKontrolPodlaKategorieVozidlaDataTableComposer
{
    /**
     * @var \Prettus\Repository\Criteria\RequestCriteria
     */
    private RequestCriteria $requestCriteria;
    /**
     * @var \Seka\Portal\Modul\Statistiky\Repositories\ZaznamRepositoryContract
     */
    private ZaznamRepositoryContract $zaznamRepository;

    /**
     * PekZoznamPoctuKontrolPodlaKategorieVozidlaDataTableComposer constructor.
     *
     * @param \Seka\Portal\Modul\Statistiky\Repositories\ZaznamRepositoryContract $zaznamRepository
     * @param \Prettus\Repository\Criteria\RequestCriteria                        $requestCriteria
     */
    public function __construct(ZaznamRepositoryContract $zaznamRepository, RequestCriteria $requestCriteria)
    {
        $this->zaznamRepository = $zaznamRepository;
        $this->requestCriteria = $requestCriteria;
    }

    /**
     * @param \Illuminate\Contracts\View\View $view
     *
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     */
    public function compose(View $view): void
    {
        $view->with('PekZoznamPoctuKontrolPodlaKategorieVozidlaDataTableComposer', $this->getData());
    }

    /**
     * @return array
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     */
    private function getData(): array
    {
        $pekId = auth()->user()->pracovisko->id;
        $this->zaznamRepository->pushCriteria($this->requestCriteria);

        $statData = $this->zaznamRepository
            ->setPresenter(ZaznamDataTablePresenter::class)
            ->findWhere([
                'pracovisko_id' => $pekId,
                'subjekt_druh' => DruhSubjektuZaznamuEnum::PRACOVISKO(),
            ]);

        return $statData['data'][0]['zoznamPoctuKontrolPodlaKategorieVozidla'];
    }
}
