<?php

namespace App\Livewire\Backend\Informacie;

use App\Models\Informacia\Enums\StavInformacieEnum;
use App\Repositories\Eloquent\Criteria\OrderAscCriteria;
use App\Repositories\Eloquent\Criteria\OrderDescCriteria;
use App\Repositories\Eloquent\Criteria\StavInformacieCriteria;
use App\Repositories\Eloquent\Criteria\WithTrashedCriteria;
use App\Repositories\InformaciaRepositoryContract;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithPagination;

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

    private const PER_PAGE = 15;

    public int $sort = 0;

    public ?string $status = null;

    protected InformaciaRepositoryContract $informaciaRepository;

    public function boot(InformaciaRepositoryContract $informaciaRepository): void
    {
        $this->informaciaRepository = $informaciaRepository;
    }

    /**
     * @throws \Prettus\Repository\Exceptions\RepositoryException
     */
    public function render(): View
    {
        $informacie = $this->informaciaRepository
            ->pushCriteria(WithTrashedCriteria::class)
            ->pushCriteria(($this->sort == 1) ? OrderAscCriteria::class : OrderDescCriteria::class)
            ->pushCriteria(new StavInformacieCriteria(stav: StavInformacieEnum::tryFrom($this->status)))
            ->paginate(self::PER_PAGE);

        return view('livewire.backend.informacie.zoznam', compact('informacie'));
    }

    #[On('filterByStatus')]
    public function filterByStatus(?string $stav = null): void
    {
        $this->status = StavInformacieEnum::tryFrom($stav)?->name;

        $this->resetPage();
    }
}
