using MimeKit;
namespace AIStudio.Tools.Mail;
///
/// What MailTextBuilder reads one mail from.
///
///
/// Not a whole mail on purpose: the sync fetches the header block and the structure first, and
/// the text parts only of a mail which turns out readable. The attachments themselves are never
/// part of this, only their names.
///
/// The complete header block of the mail.
/// The HTML part a reader sees, decoded from its transfer encoding and charset, or null when there is none.
/// The plain text part, decoded likewise, or null when there is none.
/// The file names of the attachments, in their order, without the signature of a signed mail.
/// How the structure of the mail says its content is encrypted, see MailEncryptionDetection.
public sealed record MailTextSource(HeaderList Headers, string? HtmlBody, string? TextBody, IReadOnlyList AttachmentNames, MailEncryptionKind StructureEncryption)
{
///
/// Takes everything from a mail which was fetched or loaded as a whole.
///
/// The parsed mail.
/// The source for MailTextBuilder.
public static MailTextSource FromMessage(MimeMessage message)
{
var attachmentNames = message.Attachments
.Where(attachment => !MailAttachmentRules.IsSignature(attachment.ContentType))
.Select(attachment => attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name ?? string.Empty)
.Where(name => !string.IsNullOrWhiteSpace(name))
.ToList();
return new(message.Headers, message.HtmlBody, message.TextBody, attachmentNames, MailEncryptionDetection.Detect(message.Body));
}
}