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

/// <summary>
/// A flattened instance config staged for a single in-flight deployment, fetched
/// by the site over HTTP instead of being shipped inside an Akka message (avoids
/// the 128 KB frame limit). Keyed by DeploymentId; at most one row per InstanceId
/// (a newer deploy supersedes the prior pending row). Carries a per-deployment
/// fetch token and a TTL. Promoted to DeployedConfigSnapshot on success; purged
/// on failure/timeout/TTL.
/// </summary>
public class PendingDeployment
{
    /// <summary>Primary key.</summary>
    public int Id { get; set; }

    /// <summary>Unique deployment identifier assigned at deploy time; the fetch key for the staged config.</summary>
    public string DeploymentId { get; set; }

    /// <summary>Foreign key to the owning <c>Instance</c> entity. At most one pending row per instance.</summary>
    public int InstanceId { get; set; }

    /// <summary>Revision hash of the flattened configuration, used for staleness detection.</summary>
    public string RevisionHash { get; set; }

    /// <summary>JSON-serialized flattened configuration staged for the site to fetch.</summary>
    public string ConfigurationJson { get; set; }

    /// <summary>Per-deployment fetch token presented by the site to authorize the HTTP fetch.</summary>
    public string Token { get; set; }

    /// <summary>UTC timestamp when this pending row was created.</summary>
    public DateTimeOffset CreatedAtUtc { get; set; }

    /// <summary>UTC timestamp after which this pending row is considered expired (TTL) and may be purged.</summary>
    public DateTimeOffset ExpiresAtUtc { get; set; }

    /// <summary>Initializes a new pending deployment with its identity, staged configuration, fetch token, and TTL.</summary>
    /// <param name="deploymentId">Unique deployment identifier (fetch key).</param>
    /// <param name="instanceId">Owning instance identifier.</param>
    /// <param name="revisionHash">Revision hash of the flattened configuration.</param>
    /// <param name="configurationJson">JSON-serialized flattened configuration.</param>
    /// <param name="token">Per-deployment fetch token.</param>
    /// <param name="createdAtUtc">UTC creation timestamp.</param>
    /// <param name="expiresAtUtc">UTC expiry timestamp (TTL).</param>
    public PendingDeployment(
        string deploymentId, int instanceId, string revisionHash,
        string configurationJson, string token,
        DateTimeOffset createdAtUtc, DateTimeOffset expiresAtUtc)
    {
        DeploymentId = deploymentId ?? throw new ArgumentNullException(nameof(deploymentId));
        InstanceId = instanceId;
        RevisionHash = revisionHash ?? throw new ArgumentNullException(nameof(revisionHash));
        ConfigurationJson = configurationJson ?? throw new ArgumentNullException(nameof(configurationJson));
        Token = token ?? throw new ArgumentNullException(nameof(token));
        CreatedAtUtc = createdAtUtc;
        ExpiresAtUtc = expiresAtUtc;
    }
}
