using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Domain.Engine;
///
/// The immutable state the decision point reads. Built off the request path
/// on a TTL and swapped atomically, so a request never touches the database.
///
/// Blocks are split by provenance rather than by table: the two kinds are
/// looked up in a fixed order and a clear operation must be able to name one
/// kind without touching the other.
///
public sealed record EdgeSnapshot
{
public required DateTimeOffset GeneratedAt { get; init; }
public required EdgePolicy Policy { get; init; }
public IReadOnlyDictionary OperatorBlocks { get; init; }
= new Dictionary(StringComparer.Ordinal);
public IReadOnlyDictionary EngineBans { get; init; }
= new Dictionary(StringComparer.Ordinal);
/// The operator's range bans (OPEN-66), in the prefix-bucketed
/// shape the request path can afford to ask. Kept out of
/// because a range is not a key: it is a
/// containment claim, and matching it by key would answer "is this address
/// exactly this network address" rather than "is it inside it".
public NetworkBanIndex OperatorRanges { get; init; } = NetworkBanIndex.Empty;
public IReadOnlyList AllowEntries { get; init; } = [];
public IReadOnlyDictionary Countries { get; init; }
= new Dictionary(StringComparer.OrdinalIgnoreCase);
/// Operator-written request rules, in the order the operator wrote
/// them. The first live match wins, so the listing an operator reads is the
/// evaluation order — a list whose answer depended on insertion order
/// hidden inside a dictionary would be unreadable.
public IReadOnlyList RequestRules { get; init; } = [];
/// The operator's per-path budgets (OPEN-68), in the order they were
/// written. They are not part of : a budget is a
/// count, and a count has to be kept while the requests arrive rather than
/// read from a snapshot built between them. The snapshot carries the rules so
/// the guard sees the same list the operator sees, and sees it without a
/// database read.
public IReadOnlyList RateRules { get; init; } = [];
public static EdgeSnapshot Empty(EdgePolicy policy)
=> new() { GeneratedAt = DateTimeOffset.MinValue, Policy = policy };
}