<?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\NezverejnenaUdalostBolaZverejnena;
use Seka\Portal\Modul\Terminy\Notifications\Udalost\ZverejnenaNovaUdalostNotifikaciaDB;
use Seka\Portal\Modul\Terminy\Notifications\Udalost\ZverejnenaNovaUdalostNotifikaciaMail;

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

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

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

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