///|
/// Offsets share a relative coordinate system, with no UTC anchor.
/// solve returns super-source coordinates; analyze uses a component reference.
#derive(Debug, Eq)
pub struct ClockOffset {
  clock_id : String
  offset_us : Int64
}

///|
/// Index into ConstraintSolution.constraints, and (for solve) into
/// SolveReport.input.constraints. The original constraint is retained.
#derive(Debug, Eq)
pub struct CycleEdge {
  constraint_index : Int
  constraint : @model.DifferenceConstraint
}

///|
/// Edges are in forward traversal order. The last edge returns to the first.
/// This is one conflict witness, not necessarily a minimum conflict.
#derive(Debug, Eq)
pub struct NegativeCycle {
  edges : ReadOnlyArray[CycleEdge]
  total_weight_us : Int64
}

///|
#derive(Debug, Eq)
pub(all) enum Feasibility {
  Feasible(offsets~ : ReadOnlyArray[ClockOffset])
  Infeasible(cycle~ : NegativeCycle)
}

///|
#derive(Debug, Eq)
pub struct SolveStats {
  clock_count : Int
  constraint_count : Int
  passes : Int
  edge_visits : Int64
}

///|
/// Original edge order is preserved. Independent clocks are sorted by name.
/// A feasible result covers only these supplied constraints, not log coverage.
#derive(Debug, Eq)
pub struct ConstraintSolution {
  clocks : ReadOnlyArray[String]
  constraints : ReadOnlyArray[@model.DifferenceConstraint]
  outcome : Feasibility
  stats : SolveStats
}

///|
/// Includes missing/ambiguous correlation groups and full edge evidence.
#derive(Debug, Eq)
pub struct SolveReport {
  input : @constraints.ConstraintSet
  solution : ConstraintSolution
}

///|
#derive(Debug, Eq)
pub(all) enum SolvePhase {
  Relaxation
  FeasibleVerification
  CycleWeightSum
  UpperBoundPaths
  LowerBoundPaths
}

///|
/// Errors carry no feasibility verdict or partial solution.
#derive(Debug, Eq)
pub(all) suberror SolveError {
  InvalidLimit(String)
  ClockLimitExceeded(Int)
  ConstraintLimitExceeded(Int)
  EmptyClockId
  UnknownReferenceClock(String)
  OffsetOverflow(clock_id~ : String, reference_clock~ : String)
  BoundOverflow(clock_id~ : String, lower_bound~ : Bool)
  EdgeVisitLimitExceeded(Int64)
  ArithmeticOverflow(phase~ : SolvePhase, constraint_index~ : Int)
  InvalidWitness
}