using GmRelay.Bot.Infrastructure.Telegram;
using GmRelay.Shared.Platform;

namespace GmRelay.Bot.Features.Notifications;

public sealed record DirectNotificationRecipient(long TelegramId, string DisplayName);

public sealed class DirectSessionNotificationSender(
    IPlatformMessenger messenger,
    ILogger<DirectSessionNotificationSender> logger)
{
    public async Task SendAsync(
        IEnumerable<DirectNotificationRecipient> recipients,
        string htmlText,
        string notificationKind,
        Guid sessionId,
        CancellationToken ct)
    {
        foreach (var recipient in recipients)
        {
            try
            {
                await messenger.SendPrivateMessageAsync(
                    new PlatformPrivateMessage(
                        TelegramPlatformIds.User(recipient.TelegramId, recipient.DisplayName),
                        htmlText),
                    ct);
            }
            catch (Exception ex)
            {
                logger.LogWarning(
                    ex,
                    "Failed to send {NotificationKind} DM for session {SessionId} to player {TelegramId} ({DisplayName})",
                    notificationKind,
                    sessionId,
                    recipient.TelegramId,
                    recipient.DisplayName);
            }
        }
    }
}
