<?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
 */
final class Zoznam extends Component
{
    use WithPagination;

    private const PER_PAGE = 15;

    public int $sort = 0;

    public Informacia $informacia;

    protected string $paginationTheme = 'bootstrap';

    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);
    }
}
