<?php

namespace App\Listeners;

use App\Events\NotificationEvent;
use App\Services\Firebase\FirebaseNotificationService;

class NotificationListener
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(NotificationEvent $event): void
    {

        $user = $event->user;

        $type = $event->type;

        $title = $event->title;

        $message = $event->message;

        $user->notifications()->create([
            'type' => $type,
            'data' => $message,
        ]);

        if ($user->fcm_token && config('extra_service.system_config.push_notification.is_enabled')) {
            $pushNotification = FirebaseNotificationService::getInstance();

            $pushNotification->sendNotification(
                $title,
                $message,
                $user->fcm_token
            );
        }
    }
}
