<?php

declare(strict_types=1);

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

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\UdalostDTO;
use Seka\Portal\Modul\Terminy\Notifications\MAAG2MessageDataBuilder;

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

    /**
     * @var \Seka\Portal\Modul\Terminy\Entities\Dtos\UdalostDTO
     */
    public UdalostDTO $udalost;

    /**
     * ZverejnenaNovaUdalostNotifikaciaDB constructor.
     *
     * @param \Seka\Portal\Modul\Terminy\Entities\Dtos\UdalostDTO $udalost
     */
    public function __construct(UdalostDTO $udalost)
    {
        $this->udalost = $udalost;
    }

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

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

    /**
     * @param $notifiable
     *
     * @return array
     */
    public function toDatabase($notifiable): array
    {
        return $this->udalost->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->udalost->id],
        );

        return FcmMessage::create()
            ->setData($data->toArray())
            ->setNotification(
                FcmNotification::create()
                    ->setTitle('Zverejnená nová udalosť')
                    ->setBody(
                        sprintf(
                            'Bola zverejnená udalosť %s',
                            $this->udalost->nazov,
                        ),
                    ),
            )
            ->setAndroid(AndroidConfig::create()->setData(['badge' => "$unreadNotificationsCount",]))
            ->setApns(ApnsConfig::create()->setPayload(['aps' => ['badge' => $unreadNotificationsCount,]]));
    }
}
