<?php

namespace App\Http\Livewire\Components\Informacia\Hodnotenie\Components;

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
 *
 * @package App\Http\Livewire\Components\Informacia\Hodnotenie\Components
 */
final class Zoznam extends Component
{
    use WithPagination;

    /**
     *
     */
    private const PER_PAGE = 15;

    /**
     * @var int
     */
    public int $sort = 0;

    /**
     * @var \App\Models\Informacia\Informacia
     */
    public Informacia $informacia;

    /**
     * @var string
     */
    protected string $paginationTheme = 'bootstrap';

    /**
     * @var \App\Repositories\HodnotenieRepositoryContract
     */
    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('pages.sprava.informacie.hodnotenia.livewire.components.zoznam')->withHodnotenia($hodnotenia);
    }
}
