using salesbook.Shared.Core.Interface;
using salesbook.Shared.Core.Interface.IntegryApi;
using Shiny;
using Shiny.Notifications;
using Shiny.Push;

namespace salesbook.Maui.Core.System.Notification;

public class FirebaseNotificationService(
    IPushManager pushManager,
    IIntegryRegisterNotificationRestClient integryRegisterNotificationRestClient,
    INotificationManager notificationManager
) : IFirebaseNotificationService
{
    public async Task InitFirebase()
    {
        CreateNotificationChannel();
        
        var (accessState, token) = await pushManager.RequestAccess();

        if (accessState == AccessState.Denied || token is null) return;
        await integryRegisterNotificationRestClient.Register(token);
    }

    private void CreateNotificationChannel()
    {
        var channel = new Channel
        {
            Identifier = "salesbook_push",
            Description = "Notifiche push di SalesBook",
            Importance = ChannelImportance.High,
            Actions = []
        };

        notificationManager.AddChannel(channel);
    }
}