<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Terminy\Listeners\Udalost;

use App\Models\Pouzivatel\Pouzivatel;
use App\Models\Pracovisko\Pracovisko;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Notification;
use Seka\Portal\Modul\Terminy\Entities\Dtos\UdalostDTO;
use Seka\Portal\Modul\Terminy\Events\Udalost\ZverejnenaUdalostBolaNezverejnena;
use Seka\Portal\Modul\Terminy\Notifications\Udalost\ZrusenaUdalostNotifikaciaDB;
use Seka\Portal\Modul\Terminy\Notifications\Udalost\ZrusenaUdalostNotifikaciaMail;

/**
 * Class NezverejnitZverejnenuUdalostNotifikacia
 *
 * @package Seka\Portal\Modul\Terminy\Listeners\Udalost
 */
final class NezverejnitZverejnenuUdalostNotifikacia implements ShouldQueue
{
    /**
     * @param \Seka\Portal\Modul\Terminy\Events\Udalost\ZverejnenaUdalostBolaNezverejnena $event
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Spatie\LaravelData\Exceptions\CannotCreateData
     */
    public function handle(ZverejnenaUdalostBolaNezverejnena $event): void
    {
        if ($event->udalost->nieJePovoleneUpozornenie()) {
            return;
        }

        $udalost = UdalostDTO::from($event->udalost);

        Notification::send(
            Pouzivatel::query()->aktivny()->get(),
            new ZrusenaUdalostNotifikaciaDB($udalost),
        );

        Notification::send(
            collect(
                Pracovisko::query()->get(),
            )->merge(
                Pouzivatel::query()->where('email', '!=', null)->aktivny()->get(),
            ),
            new ZrusenaUdalostNotifikaciaMail($udalost),
        );
    }
}
