<?php

namespace App\Livewire\Backend\Informacie\Hodnotenia;

use App\Models\Informacia\Informacia;
use App\Repositories\Eloquent\Criteria\OrderByUpdatedAtAscCriteria;
use App\Repositories\Eloquent\Criteria\OrderByUpdatedAtDescCriteria;
use App\Repositories\HodnotenieRepositoryContract;
use Livewire\Component;
use Livewire\WithPagination;
use Prettus\Repository\Exceptions\RepositoryException;

/**
 * Class Zoznam
 */
final class Zoznam extends Component
{
    use WithPagination;

    private const PER_PAGE = 15;

    public int $sort = 0;

    public Informacia $informacia;

    private HodnotenieRepositoryContract $hodnotenieRepository;

    /**
     * Zoznam constructor.
     */
    public function boot(HodnotenieRepositoryContract $hodnotenieRepository): void
    {
        $this->hodnotenieRepository = $hodnotenieRepository;
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     *
     * @throws RepositoryException
     */
    public function render()
    {
        $hodnotenia = $this->hodnotenieRepository
            ->pushCriteria(
                $this->sort ? OrderByUpdatedAtAscCriteria::class : OrderByUpdatedAtDescCriteria::class,
            )
            ->filterHodnoteniHodnotenehoZaznamu($this->informacia)
            ->paginate(self::PER_PAGE);

        return view('livewire.backend.informacie.hodnotenia.zoznam')->withHodnotenia($hodnotenia);
    }
}
