using Microsoft.Extensions.Logging; using UniVerse.Application.DTOs.Notifications; using UniVerse.Application.Interfaces; namespace UniVerse.Infrastructure.Notifications; public class NotificationService : INotificationService { private readonly IEnumerable _providers; private readonly INotificationScheduler _scheduler; private readonly ILogger _logger; public NotificationService( IEnumerable providers, INotificationScheduler scheduler, ILogger logger) { _providers = providers; _scheduler = scheduler; _logger = logger; } public async Task SendAsync(NotificationMessage message, CancellationToken cancellationToken = default) { ArgumentException.ThrowIfNullOrWhiteSpace(message.Channel); ArgumentException.ThrowIfNullOrWhiteSpace(message.Recipient); ArgumentException.ThrowIfNullOrWhiteSpace(message.Subject); ArgumentException.ThrowIfNullOrWhiteSpace(message.Body); var provider = _providers.FirstOrDefault(p => string.Equals(p.Channel, message.Channel, StringComparison.OrdinalIgnoreCase)) ?? throw new InvalidOperationException($"Notification provider for channel '{message.Channel}' is not registered."); _logger.LogInformation("Dispatching notification through {Channel} to {Recipient}", message.Channel, message.Recipient); await provider.SendAsync(message, cancellationToken); } public Task ScheduleAsync(ScheduleNotificationRequest request, CancellationToken cancellationToken = default) { var message = new NotificationMessage( request.Channel, request.Recipient, request.Subject, request.Body, request.RecipientName, request.Metadata); return _scheduler.ScheduleAsync(message, request.SendAt, cancellationToken); } }