<?php

namespace App\Notifications;

use App\Dtos\InformaciaDto;
use App\Mail\Informacia\ZverejnenaInformaciaPrePouzivatelaMail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

/**
 * Class InformaciaPouzivatelNotification
 *
 * Sluzi na odosielanie notifikacii o novych informaciach
 *
 * @package App\Notifications
 * @author Radoslav Lukac <dev@seka.sk>
 */
class InformaciaPouzivatelNotification extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * @var InformaciaDto
     */
    private InformaciaDto $informacia;

    /**
     * Vytvori novu instanciu notifikacie
     *
     * @param InformaciaDto $informacia Informacia pre notifikaciu
     */
    public function __construct(InformaciaDto $informacia)
    {
        $this->informacia = $informacia;
    }

    /**
     * Ziska kanaly pre dorucenie notifikacie
     *
     * @param mixed $notifiable Notifikovatelny objekt
     * @return array Pole kanalov pre dorucenie
     */
    public function via($notifiable): array
    {
        $via = [
            'database',
        ];

        if (!empty($notifiable->email) && !empty(trim($notifiable->email))) {
            $via[] = 'mail';
        }

        return $via;
    }

    /**
     * @return string[]
     */
    public function viaQueues(): array
    {
        return [
            'mail' => 'email-notifications-queue',
        ];
    }

    /**
     * Ziska mailovu reprezentaciu notifikacie
     *
     * @param mixed $notifiable Notifikovatelny objekt
     * @return Mailable Sprava pre odoslanie mailom
     */
    public function toMail($notifiable): Mailable
    {
        return (new ZverejnenaInformaciaPrePouzivatelaMail($this->informacia))->to($notifiable->email);
    }

    /**
     * Ziska reprezentaciu notifikacie pre ulozenie do databazy
     *
     * @param $notifiable
     * @return array Pole s datami notifikacie pre databazu
     */
    public function toDatabase($notifiable): array
    {
        return $this->informacia->toArray();
    }

    /**
     * Ziska reprezentaciu notifikacie v podobe pola
     *
     * @param mixed $notifiable Notifikovatelny objekt
     * @return array Pole s datami notifikacie
     */
    public function toArray($notifiable): array
    {
        return [
            'informacia_id' => $this->informacia->id,
            'titulok' => $this->informacia->titulok,
            'obsah' => $this->informacia->obsah,
        ];
    }
}
