<?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 PekZoznamPoctuKontrolPodlaDruhuAHodnoteniaDataTableComposer
 *
 * @package Seka\Portal\Modul\Statistiky\Http\View\Composers
 */
final class PekZoznamPoctuKontrolPodlaDruhuAHodnoteniaDataTableComposer
{
    /**
     * @var \Prettus\Repository\Criteria\RequestCriteria
     */
    private RequestCriteria $requestCriteria;
    /**
     * @var \Seka\Portal\Modul\Statistiky\Repositories\ZaznamRepositoryContract
     */
    private ZaznamRepositoryContract $zaznamRepository;

    /**
     * PekZoznamPoctuKontrolPodlaDruhuAHodnoteniaDataTableComposer 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('PekZoznamPoctuKontrolPodlaDruhuAHodnoteniaDataTableComposer', $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]['zoznamPoctuKontrolPodlaDruhuAHodnotenia'];
    }
}
