using GmRelay.Shared.Platform;
using Microsoft.Extensions.Logging;

namespace GmRelay.Shared.Features.Notifications;

public sealed class PlatformDirectNotificationSender(
    IPlatformMessenger messenger,
    ILogger<PlatformDirectNotificationSender> logger)
{
    public async Task SendAsync(
        PlatformDirectSessionNotificationKind kind,
        IEnumerable<PlatformUser> recipients,
        Guid sessionId,
        string title,
        DateTime scheduledAt,
        string? joinLink,
        string? actorDisplayName,
        string? reason,
        CancellationToken ct)
    {
        foreach (var recipient in recipients)
        {
            try
            {
                await messenger.SendDirectSessionNotificationAsync(
                    new PlatformDirectSessionNotification(
                        kind,
                        recipient,
                        sessionId,
                        title,
                        scheduledAt,
                        joinLink,
                        actorDisplayName,
                        reason),
                    ct);
            }
            catch (Exception ex)
            {
                logger.LogWarning(
                    ex,
                    "Failed to send {NotificationKind} notification for session {SessionId} to {Platform} user {ExternalUserId} ({DisplayName})",
                    kind,
                    sessionId,
                    recipient.Platform,
                    recipient.ExternalUserId,
                    recipient.DisplayName);
            }
        }
    }
}
