<?php

declare(strict_types=1);

namespace App\Notifications\Push;

use App\Dtos\PouzivatelDto;
use Illuminate\Bus\Queueable;
use NotificationChannels\Fcm\Exceptions\CouldNotSendNotification;
use NotificationChannels\Fcm\FcmMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\AndroidNotification;
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\MessageDataBuilder;
use Seka\Portal\Core\Notifications\FirebaseCloudMessaging\Messages\Data\Enums\MessageDataTypeEnum;
use Seka\Portal\Core\Notifications\FirebaseCloudMessaging\Messages\Data\Enums\MessageDataMethodEnum;
use Seka\Portal\Core\Notifications\FirebaseCloudMessaging\Messages\Data\Enums\MessageDataSourceEnum;

/**
 * Class MAAISEKUserActivatedPushNotification
 *
 * @package App\Notifications\Push
 */
final class MAAISEKUserActivatedPushNotification extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * @var \App\Dtos\PouzivatelDto
     */
    private PouzivatelDto $pouzivatel;

    /**
     * @param \App\Dtos\PouzivatelDto $pouzivatel
     */
    public function __construct(PouzivatelDto $pouzivatel)
    {
        $this->pouzivatel = $pouzivatel;
    }

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

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

    /**
     * @param $notifiable
     *
     * @return \NotificationChannels\Fcm\FcmMessage
     * @throws CouldNotSendNotification
     */
    public function toFcm($notifiable): FcmMessage
    {
        $unreadNotificationsCount = $notifiable->unreadNotifications()->count();
        $data = MessageDataBuilder::make(
            MessageDataSourceEnum::PORTAL_WEB(),
            MessageDataTypeEnum::INTERACTIVE(),
            MessageDataMethodEnum::MAA_SHOW_USER_PROFILE_SCREEN()
        );

        return FcmMessage::create()
            ->setData($data->toArray())
            ->setNotification(FCMNotification::create()
                ->setTitle('Aktivácia používateľa')
                ->setBody(sprintf('V apke bol aktivovaný používateľ %s.', $this->pouzivatel->cele_meno)))
            ->setAndroid(AndroidConfig::create()->setNotification(AndroidNotification::create()->setNotificationCount($unreadNotificationsCount)))
            ->setApns(ApnsConfig::create()->setPayload(['aps' => ['badge' => $unreadNotificationsCount,]]));
    }
}
