<?php

declare(strict_types=1);

namespace Seka\Vzdelavanie\Http\Livewire\Pages\Sprava\Testovanie\Pouzivatelia\Components;

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

/**
 * Class Zoznam
 *
 * @package Seka\Vzdelavanie\Http\Livewire\Pages\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 string
     */
    protected string $paginationTheme = 'bootstrap';
    /**
     * @var \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\Paginator
     */
    protected $testy;
    /**
     * @var string[]
     */
    protected $listeners = [
        'filterByText',
        'filterByStatus',
        'filterByPekCode',
    ];

    /**
     * @return \Illuminate\Contracts\View\View
     * @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.pages.sprava.testovanie.pouzivatelia.components.zoznam')
            ->withTesty($this->testy);
    }

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

    /**
     * @param string|null $stav
     */
    public function filterByStatus(string $stav = null): void
    {
        $this->status = $stav;

        $this->resetPage();
    }

    /**
     * @param string|null $kodPek
     */
    public function filterByPekCode(string $kodPek = null): void
    {
        $this->kodPek = $kodPek;

        $this->resetPage();
    }

    /**
     * @param string|null $value
     */
    public function filterByText(string $value = null): void
    {
        $this->searchText = $value;

        $this->resetPage();
    }
}
