///|
/// Administrative outcome of a lease command.
pub(all) enum DecisionKind {
  Acquired
  Renewed
  Released
  Transferred
  Reaped
  Rejected
} derive(Debug, Eq)

///|
/// A granted lease. `token` is a monotonically increasing fencing token.
pub(all) struct Lease {
  resource : String
  holder : String
  token : Int
  revision : Int
  issued_at : Int
  expires_at : Int
} derive(Debug, Eq)

///|
/// Complete deterministic state for one leased resource.
pub(all) struct LeaseState {
  resource : String
  lease : Lease?
  next_token : Int
  revision : Int
} derive(Debug, Eq)

///|
/// Commands accepted by the pure lease state machine.
pub(all) enum LeaseCommand {
  Acquire(String, Int, Int?)
  Renew(String, Int, Int)
  Release(String, Int)
  Transfer(String, Int, String, Int)
  Reap
} derive(Debug, Eq)

///|
/// An auditable state transition record.
pub(all) struct LeaseEvent {
  resource : String
  kind : DecisionKind
  at : Int
  holder : String
  token : Int
  revision : Int
  detail : String
} derive(Debug, Eq)

///|
/// Result returned for every command, including rejected commands.
pub(all) struct LeaseDecision {
  state : LeaseState
  accepted : Bool
  kind : DecisionKind
  lease : Lease?
  event : LeaseEvent?
  message : String
} derive(Debug, Eq)

///|
/// A write request protected by a lease fencing token.
pub(all) struct FencedWrite {
  resource : String
  holder : String
  token : Int
  operation : String
} derive(Debug, Eq)

///|
/// Highest fencing token observed for one resource.
pub(all) struct FenceMark {
  resource : String
  token : Int
  holder : String
} derive(Debug, Eq)

///|
/// Storage-independent fencing state.
pub(all) struct FenceGuard {
  marks : Array[FenceMark]
} derive(Debug, Eq)

///|
/// Result of validating a fenced write.
pub(all) struct FenceDecision {
  next_guard : FenceGuard
  accepted : Bool
  highest_token : Int
  message : String
} derive(Debug, Eq)

///|
/// Multi-resource lease coordinator state.
pub(all) struct LeaseTable {
  states : Array[LeaseState]
  events : Array[LeaseEvent]
  accepted_commands : Int
  rejected_commands : Int
} derive(Debug, Eq)

///|
/// Result of applying a command to a resource table.
pub(all) struct TableDecision {
  table : LeaseTable
  decision : LeaseDecision
} derive(Debug, Eq)

///|
/// A command with its resource and deterministic logical timestamp.
pub(all) struct TimedCommand {
  resource : String
  command : LeaseCommand
  at : Int
} derive(Debug, Eq)

///|
/// Summary for a deterministic command batch.
pub(all) struct BatchResult {
  table : LeaseTable
  decisions : Array[LeaseDecision]
  accepted : Int
  rejected : Int
} derive(Debug, Eq)

///|
/// Candidate used by deterministic local election policy.
pub(all) struct Candidate {
  id : String
  priority : Int
} derive(Debug, Eq)

///|
/// One cluster member's vote for a term.
pub(all) struct ElectionVote {
  voter : String
  candidate : String
  term : Int
  granted : Bool
} derive(Debug, Eq)

///|
/// Internal vote accumulator.
priv struct VoteTally {
  candidate : String
  voters : Array[String]
}

///|
/// Evidence that one candidate reached quorum in a term.
pub(all) struct QuorumCertificate {
  valid : Bool
  leader : String
  term : Int
  quorum : Int
  voters : Array[String]
  equivocations : Array[String]
  message : String
} derive(Debug, Eq)

///|
/// Point-in-time table metrics.
pub(all) struct LeaseMetrics {
  resources : Int
  stored_leases : Int
  active_leases : Int
  expired_leases : Int
  highest_token : Int
  events : Int
  accepted_commands : Int
  rejected_commands : Int
} derive(Debug, Eq)

///|
/// One invariant violation found in an event trace.
pub(all) struct AuditFinding {
  code : String
  resource : String
  event_index : Int
  message : String
} derive(Debug, Eq)

///|
/// Deterministic safety audit result.
pub(all) struct AuditReport {
  valid : Bool
  checked_events : Int
  findings : Array[AuditFinding]
} derive(Debug, Eq)

///|
/// Reproducible failover simulation evidence.
pub(all) struct SimulationReport {
  cycles : Int
  leases_acquired : Int
  writes_accepted : Int
  stale_writes_rejected : Int
  final_token : Int
  audit_valid : Bool
  evidence : Int
} derive(Debug, Eq)

///|
/// Validation issue for state restored from external storage.
pub(all) struct StateIssue {
  code : String
  message : String
} derive(Debug, Eq)