namespace ZB.MOM.WW.ScadaBridge.NotificationService;

/// <summary>
/// The email delivery transports the service supports. The configured
/// <c>SmtpConfiguration.Transport</c> string is parsed into this enum to choose
/// which client actually submits the message.
/// </summary>
public enum EmailTransport
{
    /// <summary>Direct SMTP submission via MailKit (the original path).</summary>
    Smtp,

    /// <summary>On-prem Exchange Web Services — one CreateItem SOAP call over HTTPS.</summary>
    Ews,
}

/// <summary>
/// Parses the free-text <c>SmtpConfiguration.Transport</c> value into an
/// <see cref="EmailTransport"/>, rejecting unknown values rather than silently
/// falling back to SMTP.
/// </summary>
public static class EmailTransportParser
{
    /// <summary>
    /// Parses a configured transport string. A null or empty value defaults to
    /// <see cref="EmailTransport.Smtp"/> — every pre-EWS configuration row stores no
    /// transport and is an SMTP row. An unknown value is a configuration error that
    /// retrying cannot fix, so it throws and the delivery adapter surfaces it as a
    /// permanent failure (mirrors <see cref="SmtpTlsModeParser"/>).
    /// </summary>
    /// <param name="transport">The transport string to parse (Smtp or Ews); null/empty defaults to Smtp.</param>
    /// <exception cref="ArgumentException">The value is not one of Smtp/Ews.</exception>
    /// <returns>The corresponding <see cref="EmailTransport"/> enum value.</returns>
    public static EmailTransport Parse(string? transport)
    {
        if (string.IsNullOrWhiteSpace(transport))
        {
            return EmailTransport.Smtp;
        }

        return transport.Trim().ToLowerInvariant() switch
        {
            "smtp" => EmailTransport.Smtp,
            "ews" => EmailTransport.Ews,
            _ => throw new ArgumentException(
                $"Unknown email transport '{transport}'. Expected one of: Smtp, Ews.",
                nameof(transport)),
        };
    }
}
