namespace OpenWaf.Domain.Security;
///
/// A delivery this edge owes somebody (OPEN-60).
///
/// The alert table is the record of what happened; this table is the record of
/// whether anyone was told. They are separate rows because they fail separately:
/// an alert that is durably written and never pushed is exactly the blindness
/// the webhook was added to remove, and until now the queue of "not yet pushed"
/// lived in a channel in one process — three attempts, then the delivery was
/// gone for good, and a restart dropped everything still waiting.
///
/// The payload is copied here rather than referenced. Retention prunes
/// edge_alerts on the ban-retention window, and a push that is still due
/// must not die with the row it came from: an alert raised at 03:00 against an
/// endpoint that is down until the morning is the case this exists for.
///
public sealed class AlertPush
{
public long Id { get; init; }
/// The alert this delivery came from. Not a foreign key and not
/// unique-by-constraint across restarts — it is the answer to "have I already
/// promised this alert to somebody", and it is what an operator reads next to
/// the alert row.
public long AlertId { get; set; }
// ---- the payload, copied (see the class note) ----
public string Kind { get; set; } = string.Empty;
public string SourceKey { get; set; } = string.Empty;
public int Count { get; set; }
public int? LastStatusCode { get; set; }
public string? LastPath { get; set; }
public string Detail { get; set; } = string.Empty;
public DateTimeOffset AlertCreatedAt { get; set; } = DateTimeOffset.UtcNow;
// ---- the delivery state ----
public int Attempts { get; set; }
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
/// When this row may be tried again. Null while a process holds a
/// claim on it.
public DateTimeOffset? NextAttemptAt { get; set; }
public string? LastError { get; set; }
/// Set once, when the endpoint accepted the push. A delivered row
/// keeps its row until retention ages it out, so "how many did we tell people
/// about" is a count of rows rather than a counter that resets on restart.
public DateTimeOffset? PushedAt { get; set; }
/// Set once, when the attempts ran out. The difference between this
/// and a pending row is the difference between "the endpoint is down" and
/// "we stopped asking", and an operator is entitled to know which one they
/// are reading.
public DateTimeOffset? GivenUpAt { get; set; }
/// Which process is trying this row right now. Two edges sharing one
/// database would otherwise push the same alert twice, and a duplicate page
/// at 03:00 is not a harmless bug. A claim older than the reclaim window
/// belongs to a process that died and is taken over.
public string? ClaimedBy { get; set; }
public DateTimeOffset? ClaimedAt { get; set; }
}