namespace ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;

public class SmsConfiguration
{
    /// <summary>Gets or sets the primary key.</summary>
    public int Id { get; set; }
    /// <summary>Gets or sets the Twilio Account SID.</summary>
    public string AccountSid { get; set; }
    /// <summary>
    /// Gets or sets the Twilio Auth Token (secret). Stored encrypted; null only transiently
    /// during configuration, never a valid production value.
    /// </summary>
    public string? AuthToken { get; set; }
    /// <summary>
    /// Gets or sets the sender phone number (E.164) placed in the From field, or null when
    /// delivery is via a <see cref="MessagingServiceSid"/> instead. A valid config has a
    /// FromNumber and/or a MessagingServiceSid; that either-or invariant is enforced at the
    /// management/UI boundary and again by the delivery adapter (it is not a ctor invariant
    /// because MessagingServiceSid is a settable property assigned after construction).
    /// </summary>
    public string? FromNumber { get; set; }
    /// <summary>Gets or sets the Twilio Messaging Service SID used instead of a From number, or null.</summary>
    public string? MessagingServiceSid { get; set; }
    /// <summary>Gets or sets the Twilio REST API base URL, or null to use the provider default.</summary>
    public string? ApiBaseUrl { get; set; }
    /// <summary>Gets or sets the connection timeout in seconds. Honored per-send by the SMS delivery adapter.</summary>
    public int ConnectionTimeoutSeconds { get; set; }
    /// <summary>
    /// Gets or sets the maximum number of delivery retries before parking.
    /// <para>
    /// LIVE for SMS: the Notification Outbox dispatcher selects the retry policy by
    /// notification <c>Type</c> — SMS notifications retry under this value (and
    /// <see cref="RetryDelay"/>), while Email (and every other type) uses the central SMTP
    /// policy. When no SMS configuration row exists the dispatcher falls back to the SMTP
    /// policy. A non-positive value is clamped to the outbox fallback (10) with a Warning so
    /// a misconfiguration cannot silently park SMS on the first transient failure. See
    /// <c>NotificationOutboxActor.ResolveRetryPoliciesAsync</c>.
    /// </para>
    /// </summary>
    public int MaxRetries { get; set; }
    /// <summary>
    /// Gets or sets the delay between retry attempts. LIVE for SMS — see
    /// <see cref="MaxRetries"/>: the dispatcher schedules the next SMS attempt with this delay
    /// (clamped to the 1-minute fallback if non-positive), falling back to the SMTP interval
    /// when no SMS configuration exists.
    /// </summary>
    public TimeSpan RetryDelay { get; set; }

    /// <summary>
    /// Initializes a new <see cref="SmsConfiguration"/> with the required Account SID and
    /// sensible defaults for the numeric and timeout fields. <paramref name="fromNumber"/>
    /// is optional — a Twilio Messaging-Service-only config sets <see cref="MessagingServiceSid"/>
    /// instead and leaves the From number null.
    /// </summary>
    /// <param name="accountSid">Twilio Account SID.</param>
    /// <param name="fromNumber">Sender phone number (E.164) for the From field, or null for a Messaging-Service-only config.</param>
    public SmsConfiguration(string accountSid, string? fromNumber = null)
    {
        AccountSid = accountSid ?? throw new ArgumentNullException(nameof(accountSid));
        FromNumber = fromNumber;
        ConnectionTimeoutSeconds = 30;
        MaxRetries = 10;
        RetryDelay = TimeSpan.FromMinutes(1);
    }
}
