using ScadaLink.Commons.Types.Enums;

namespace ScadaLink.StoreAndForward;

/// <summary>
/// WP-9: Represents a single store-and-forward message as stored in SQLite.
/// Maps to the sf_messages table.
/// </summary>
public class StoreAndForwardMessage
{
    /// <summary>Unique message ID (GUID).</summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>WP-9: Category: ExternalSystem, Notification, or CachedDbWrite.</summary>
    public StoreAndForwardCategory Category { get; set; }

    /// <summary>Target system name (external system, notification list, or DB connection).</summary>
    public string Target { get; set; } = string.Empty;

    /// <summary>JSON-serialized payload containing the call details.</summary>
    public string PayloadJson { get; set; } = string.Empty;

    /// <summary>
    /// Number of retry-sweep attempts performed so far. The initial (immediate or
    /// caller-made) delivery attempt is attempt 0 and is not counted here; this
    /// field counts only background retry attempts (StoreAndForward-003).
    /// </summary>
    public int RetryCount { get; set; }

    /// <summary>Maximum retry-sweep attempts before parking (0 = no limit).</summary>
    public int MaxRetries { get; set; }

    /// <summary>Retry interval in milliseconds.</summary>
    public long RetryIntervalMs { get; set; }

    /// <summary>When this message was first enqueued.</summary>
    public DateTimeOffset CreatedAt { get; set; }

    /// <summary>When delivery was last attempted (null if never attempted).</summary>
    public DateTimeOffset? LastAttemptAt { get; set; }

    /// <summary>Current status of the message.</summary>
    public StoreAndForwardMessageStatus Status { get; set; }

    /// <summary>Last error message from a failed delivery attempt.</summary>
    public string? LastError { get; set; }

    /// <summary>
    /// Instance that originated this message (for S&amp;F-survives-delete behavior).
    /// WP-13: Messages are NOT cleared when instance is deleted.
    /// </summary>
    public string? OriginInstanceName { get; set; }
}
