<?php

namespace App\Livewire\Frontend\Pouzivatel\Notifications;

use App\Enums\UpozornenieStavEnum;
use App\Models\Pouzivatel\Pouzivatel;
use App\Notifications\Push\MAAISEKUserNotificationsCountPushNotification;
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;

    public bool $unreaded = false;

    protected int $countAll = 0;

    protected int $countRead = 0;

    protected int $countNotRead = 0;

    public function mount(): void
    {
        $this->pouzivatel = auth()->user();
        $this->status = UpozornenieStavEnum::NOTREAD->value;
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function render(): mixed
    {
        /** @var Pouzivatel $pouzivatel */
        $pouzivatel = auth()->user();
        $query = $pouzivatel->notifications();

        $this->countAll = $pouzivatel->notifications()->count();
        $this->countRead = $pouzivatel->readNotifications()->count();
        $this->countNotRead = $pouzivatel->unreadNotifications()->count();

        $this->unreaded = (bool)$this->countNotRead;

        if (UpozornenieStavEnum::from($this->status) === UpozornenieStavEnum::NOTREAD) {
            $query = $query->whereNull('read_at');
        }

        if (UpozornenieStavEnum::from($this->status) === UpozornenieStavEnum::READ) {
            $query = $query->whereNotNull('read_at');
        }

        $upozornenia = $query
            ->reorder('created_at', $this->sort ? 'asc' : 'desc')
            ->paginate(self::PER_PAGE);

        return view('livewire.frontend.pouzivatel.notifications.zoznam')
            ->withUpozornenia($upozornenia);
    }

    /**
     * Oznacit vsetky neprecitane upozornenia ako precitane
     */
    public function notificationsReaded(): void
    {
        /** @var Pouzivatel $pouzivatel */
        $pouzivatel = auth()->user();
        $pouzivatel->unreadNotifications->markAsRead();
        $pouzivatel->notify(new MAAISEKUserNotificationsCountPushNotification);

        // udalost pre posluchacov inych aktivnych komponentov na aktualizaciu ich stavu
        $this->dispatch('userNotificationsReadChanged');
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function markAsRead(string $uuid, string $url)
    {
        /** @var Pouzivatel $pouzivatel */
        $pouzivatel = auth()->user();
        /** @var \Illuminate\Notifications\DatabaseNotification $upozornenie */
        $upozornenie = $pouzivatel->notifications()->findOrFail($uuid);

        // oznacit upozornenie ako precitane ak este nie je precitane
        if ($upozornenie->read() === false) {
            $upozornenie->markAsRead();
            $pouzivatel->notify(new MAAISEKUserNotificationsCountPushNotification);
        }

        // presmerovanie na zdroj upozornenia
        return redirect($url);
    }

    /**
     * Filtrovanie upozorneni podla stavu precitania upozornenia
     */
    #[On('filterByReadState')]
    public function filterByReading(string $state): void
    {
        $this->status = $state;
    }

    public function getCountByType(string $type): int
    {
        $stav = UpozornenieStavEnum::tryFrom($type);

        return match ($stav) {
            UpozornenieStavEnum::ALL => $this->countAll,
            UpozornenieStavEnum::READ => $this->countRead,
            UpozornenieStavEnum::NOTREAD => $this->countNotRead,
        };
    }
}
