<?php

declare(strict_types=1);

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

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\ApnsConfig;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
use Seka\Portal\Core\Notifications\FirebaseCloudMessaging\Channels\FcmChannel;
use Seka\Portal\Core\Notifications\FirebaseCloudMessaging\Messages\Data\Enums\MessageDataMethodEnum;
use Seka\Portal\Core\Notifications\FirebaseCloudMessaging\Messages\Data\Enums\MessageDataTypeEnum;
use Seka\Portal\Modul\Terminy\Entities\Dtos\RegistraciaDTO;
use Seka\Portal\Modul\Terminy\Mail\Registracia\ZrusenaRegistraciaNaUdalostMail;
use Seka\Portal\Modul\Terminy\Notifications\MAAG2MessageDataBuilder;

/**
 * Class ZrusenaRegistraciaNotifikacia
 *
 * @package Seka\Portal\Modul\Terminy\Notifications\Registracia
 */
class ZrusenaRegistraciaNotifikacia extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * @var \Seka\Portal\Modul\Terminy\Entities\Dtos\RegistraciaDTO
     */
    public RegistraciaDTO $registraciaDTO;

    /**
     * @param \Seka\Portal\Modul\Terminy\Entities\Dtos\RegistraciaDTO $registracia
     */
    public function __construct(RegistraciaDTO $registracia)
    {
        $this->registraciaDTO = $registracia;
    }

    /**
     * @return string[]
     */
    public function via(): array
    {
        return [
            'mail',
            'database',
            FcmChannel::class,
        ];
    }

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

    /**
     * @param $notifiable
     *
     * @return \Seka\Portal\Modul\Terminy\Mail\Registracia\ZrusenaRegistraciaNaUdalostMail
     */
    public function toMail($notifiable): ZrusenaRegistraciaNaUdalostMail
    {
        return (new ZrusenaRegistraciaNaUdalostMail($this->registraciaDTO))
            ->to($notifiable);
    }

    /**
     * @param $notifiable
     *
     * @return array
     */
    public function toDatabase($notifiable): array
    {
        return $this->registraciaDTO->toArray();
    }

    /**
     * @param $notifiable
     *
     * @return \NotificationChannels\Fcm\FcmMessage
     * @throws \NotificationChannels\Fcm\Exceptions\CouldNotSendNotification
     */
    public function toFcm($notifiable): FcmMessage
    {
        $unreadNotificationsCount = $notifiable->unreadNotifications()->count();
        $data = MAAG2MessageDataBuilder::make(
            MessageDataTypeEnum::INTERACTIVE(),
            MessageDataMethodEnum::MAA_SHOW_USER_NOTIFICATION_SCREEN(),
            ['id' => $this->registraciaDTO->id]
        );

        return FcmMessage::create()
            ->setData($data->toArray())
            ->setNotification(FcmNotification::create()
                ->setTitle('Zrušenie registrácie')
                ->setBody(sprintf(
                        'Vaša registrácia na udalosť %s bola zrušená',
                        $this->registraciaDTO->udalost->nazov)
                )
            )
            ->setAndroid(AndroidConfig::create()->setData(['badge' => "$unreadNotificationsCount",]))
            ->setApns(ApnsConfig::create()->setPayload(['aps' => ['badge' => $unreadNotificationsCount,]]));
    }
}
