using Microsoft.Extensions.DependencyInjection; using StellaOps.DependencyInjection; using StellaOps.Notify.Engine; using StellaOps.Notify.Models; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace StellaOps.Notify.Connectors.Email; [ServiceBinding(typeof(INotifyChannelTestProvider), ServiceLifetime.Singleton)] public sealed class EmailChannelTestProvider : INotifyChannelTestProvider { public NotifyChannelType ChannelType => NotifyChannelType.Email; public Task BuildPreviewAsync(ChannelTestPreviewContext context, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var subject = context.Request.Title ?? "Stella Ops Notify Preview"; var summary = context.Request.Summary ?? $"Preview generated at {context.Timestamp:O}."; var htmlBody = !string.IsNullOrWhiteSpace(context.Request.Body) ? context.Request.Body! : $"

{summary}

Trace: {context.TraceId}

"; var textBody = context.Request.TextBody ?? $"{summary}{Environment.NewLine}Trace: {context.TraceId}"; var preview = NotifyDeliveryRendered.Create( NotifyChannelType.Email, NotifyDeliveryFormat.Email, context.Target, subject, htmlBody, summary, textBody, context.Request.Locale, ChannelTestPreviewUtilities.ComputeBodyHash(htmlBody), context.Request.Attachments); var metadata = EmailMetadataBuilder.Build(context); return Task.FromResult(new ChannelTestPreviewResult(preview, metadata)); } }