<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Vzdelavanie\Livewire\Sprava\Testovanie\Pouzivatelia\Components;

use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithPagination;
use Seka\Portal\Modul\Vzdelavanie\Models\Pouzivatel\Testovanie\StavVyhodnoteniaTestuEnum;
use Seka\Portal\Modul\Vzdelavanie\Models\Pouzivatel\Testovanie\Test;

/**
 * Class Zoznam
 *
 * @package Seka\Portal\Modul\Vzdelavanie\Livewire\Sprava\Testovanie\Pouzivatelia\Components
 */
final class Zoznam extends Component
{
    use WithPagination;

    /**
     *
     */
    private const PER_PAGE = 15;
    /**
     * @var int
     */
    public $sort = 0;
    /**
     * @var string|null
     */
    public ?string $status = null;
    /**
     * @var null
     */
    public $kodPek = null;
    /**
     * @var null
     */
    public $searchText = null;
    /**
     * @var \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\Paginator
     */
    protected $testy;

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function render(): View
    {
        $query = $this->getQuery();

        if (!is_null($this->status) && StavVyhodnoteniaTestuEnum::from($this->status)) {
            $query = $query->where('stav', $this->status);
        }

        if (!empty($this->kodPek)) {
            $query = $query->whereHas('pouzivatel.pracovisko', function ($query) {
                $query->where('kod', $this->kodPek);
            });
        }

        if (!empty($this->searchText)) {
            $query = $query->whereHas('pouzivatel', function ($query) {
                $query
                    ->where('meno', 'ilike', sprintf('%%%s%%', $this->searchText))
                    ->orWhere('priezvisko', 'ilike', sprintf('%%%s%%', $this->searchText))
                    ->orWhere('login', 'ilike', sprintf('%%%s%%', $this->searchText));
            })->orWhere('nazov', 'ilike', sprintf('%%%s%%', $this->searchText));
        }

        $query = $query->orderBy('updated_at', $this->sort ? 'asc' : 'desc');

        $this->testy = $query->paginate(self::PER_PAGE);

        return view('Vzdelavanie::livewire.sprava.testovanie.pouzivatelia.components.zoznam')
            ->withTesty($this->testy);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function getQuery(): Builder
    {
        return Test::query()
            ->with('pouzivatel.pracovisko');
    }

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

        $this->resetPage();
    }

    #[On('filterByPekCode')]
    public function filterByPekCode(?string $kodPek = null): void
    {
        $this->kodPek = $kodPek;

        $this->resetPage();
    }

    #[On('filterByText')]
    public function filterByText(?string $value = null): void
    {
        $this->searchText = $value;

        $this->resetPage();
    }
}
