namespace OpenWaf.Domain.Security;
///
/// One blocked source — either an operator's manual entry or an engine ban.
/// Keyed by the normalised source key (see ),
/// not by the raw peer address, so a ban written by the engine and a block
/// typed by the operator land on the same row shape and are compared on the
/// same key at the decision point.
///
public sealed class BlockEntry
{
public long Id { get; init; }
/// Normalised source key: IPv4 dotted quad, or IPv6 /64 prefix
/// written as "a:b:c:d::/64". Unique together with .
public string SourceKey { get; set; } = string.Empty;
public BlockSource Source { get; set; }
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
/// Engine bans only. Null means "does not expire" — which only an
/// operator entry may have.
public DateTimeOffset? ExpiresAt { get; set; }
/// Operator free text, or the engine's machine-readable
/// explanation ("12 consecutive refusals 400-499 in 5 min, last 404 /wp-login.php").
public string? Reason { get; set; }
/// Per-entry override for the block response. When set, the edge
/// answers 301 to this URL instead of the kind's default. Operators use it
/// to send scanners to someone else's bandwidth; the engine never sets it.
public string? RedirectTo { get; set; }
// ---- evidence, so an automatic ban is explainable from stored rows ----
public int? LastStatusCode { get; set; }
public string? LastPath { get; set; }
public string? LastUserAgent { get; set; }
public string? LastCountryCode { get; set; }
public string? LastCity { get; set; }
public string? LastRegion { get; set; }
public int? ConsecutiveRefusals { get; set; }
/// Whether the ban is live right now. An operator entry (no
/// expiry) is live until deleted; an engine ban stops being live when it
/// ages out, without anyone having to delete it.
public bool IsLive(DateTimeOffset now) => ExpiresAt is null || ExpiresAt > now;
}