<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Terminy\Entities\Registracia;

use Seka\Portal\Modul\Terminy\Entities\Dtos\RegistraciaDTO;
use Seka\Portal\Modul\Terminy\Events\Registracia\VytvorenaRegistraciaNaUdalost;
use Seka\Portal\Modul\Terminy\Events\Registracia\ZrusenaRegistraciaNaUdalost;

/**
 * Class RegistraciaObserver
 *
 * @package Seka\Portal\Modul\Terminy\Entities\Registracia
 */
class RegistraciaObserver
{
    /**
     * Handle "retrieved" event.
     */
    public function retrieved(Registracia $registracia): void {}

    /**
     * Handle "creating" event.
     */
    public function creating(Registracia $registracia): void {}

    /**
     * Handle "created" event.
     */
    public function created(Registracia $registracia): void {}

    /**
     * Handle "saving" event.
     */
    public function saving(Registracia $registracia): void {}

    /**
     * Handle "saved" event.
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function saved(Registracia $registracia): void
    {
        $notifiable = $registracia->pouzivatel->withoutRelations() ?? $registracia->vytvoril->withoutRelations();
        $registraciaWithRelations = $registracia::with(['udalost', 'pouzivatel', 'vytvoril', 'pracovisko'])
            ->findOrFail($registracia->getKey());
        $registraciaDTO = RegistraciaDTO::from($registraciaWithRelations);
        VytvorenaRegistraciaNaUdalost::dispatch($registraciaDTO, $notifiable);
    }

    /**
     * Handle "updating" event.
     */
    public function updating(Registracia $registracia): void {}

    /**
     * Handle "updated" event.
     */
    public function updated(Registracia $registracia): void {}

    /**
     * Handle "replicating" event.
     */
    public function replicating(Registracia $registracia): void {}

    /**
     * Handle "deleting" event.
     */
    public function deleting(Registracia $registracia): void {}

    /**
     * Handle "deleted" event.
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function deleted(Registracia $registracia): void
    {
        $notifiable = $registracia->pouzivatel->withoutRelations() ?? $registracia->vytvoril->withoutRelations();
        $registraciaWithRelations = $registracia::withTrashed()
            ->with(['udalost', 'pouzivatel', 'vytvoril', 'pracovisko'])
            ->findOrFail($registracia->getKey());
        $registraciaDTO = RegistraciaDTO::from($registraciaWithRelations);
        ZrusenaRegistraciaNaUdalost::dispatch($registraciaDTO, $notifiable);
    }

    /**
     * Handle "forceDeleted" event.
     */
    public function forceDeleted(Registracia $registracia): void {}

    /**
     * Handle "restoring" event.
     */
    public function restoring(Registracia $registracia): void {}

    /**
     * Handle "restored" event.
     */
    public function restored(Registracia $registracia): void {}
}
