///|
#non_exhaustive
#derive(Debug, Eq)
pub(all) suberror ResilienceError {
  CircuitOpen(retry_after_ms~ : Int64)
  ConcurrencyLimitExceeded(max_concurrent~ : Int32)
  TimedOut(timeout_ms~ : Int32)
}

///|
#derive(Debug, Eq)
pub(all) enum CircuitState {
  Closed
  Open
  HalfOpen
}

///|
#derive(Debug, Eq)
pub(all) struct CircuitBreakerConfig {
  failure_threshold : Int32
  reset_timeout_ms : Int64
  half_open_success_threshold : Int32
  half_open_max_calls : Int32
}

///|
pub fn CircuitBreakerConfig::CircuitBreakerConfig(
  failure_threshold~ : Int32 = 5,
  reset_timeout_ms~ : Int64 = 30000L,
  half_open_success_threshold~ : Int32 = 1,
  half_open_max_calls~ : Int32 = 1,
) -> CircuitBreakerConfig {
  {
    failure_threshold,
    reset_timeout_ms,
    half_open_success_threshold,
    half_open_max_calls: if half_open_max_calls < 1 {
      1
    } else {
      half_open_max_calls
    },
  }
}

///|
/// Stateful Circuit Breaker shared across inbound routes and outbound HttpClient calls.
pub struct CircuitBreaker {
  clock : &@runtime.Clock
  config : CircuitBreakerConfig
  mut state : CircuitState
  mut consecutive_failures : Int32
  mut half_open_successes : Int32
  mut in_flight_probes : Int32
  mut half_open_epoch : Int64
  mut opened_at_ms : Int64
}

///|
pub fn CircuitBreaker::CircuitBreaker(
  clock~ : &@runtime.Clock = @runtime.SystemClock::SystemClock(),
  config~ : CircuitBreakerConfig = CircuitBreakerConfig::CircuitBreakerConfig(),
) -> CircuitBreaker {
  {
    clock,
    config,
    state: CircuitState::Closed,
    consecutive_failures: 0,
    half_open_successes: 0,
    in_flight_probes: 0,
    half_open_epoch: 0L,
    opened_at_ms: 0L,
  }
}

///|
pub fn CircuitBreaker::state(self : CircuitBreaker) -> CircuitState {
  match self.state {
    Open => {
      let now = self.clock.now_ms()
      if now - self.opened_at_ms >= self.config.reset_timeout_ms {
        self.state = CircuitState::HalfOpen
        self.half_open_successes = 0
        self.in_flight_probes = 0
        self.half_open_epoch = self.half_open_epoch + 1L
      }
      self.state
    }
    other => other
  }
}

///|
fn CircuitBreaker::allow_request_tracked(
  self : CircuitBreaker,
) -> (Bool, Int64) raise ResilienceError {
  match self.state() {
    Closed => (false, self.half_open_epoch)
    HalfOpen =>
      if self.in_flight_probes >= self.config.half_open_max_calls {
        raise ResilienceError::CircuitOpen(retry_after_ms=0L)
      } else {
        self.in_flight_probes = self.in_flight_probes + 1
        (true, self.half_open_epoch)
      }
    Open => {
      let now = self.clock.now_ms()
      let rem = self.opened_at_ms + self.config.reset_timeout_ms - now
      raise ResilienceError::CircuitOpen(
        retry_after_ms=if rem < 0L { 0L } else { rem },
      )
    }
  }
}

///|
pub fn CircuitBreaker::allow_request(
  self : CircuitBreaker,
) -> Unit raise ResilienceError {
  ignore(self.allow_request_tracked())
}

///|
pub fn CircuitBreaker::record_success(self : CircuitBreaker) -> Unit {
  match self.state {
    HalfOpen => {
      if self.in_flight_probes > 0 {
        self.in_flight_probes = self.in_flight_probes - 1
      }
      self.half_open_successes = self.half_open_successes + 1
      if self.half_open_successes >= self.config.half_open_success_threshold {
        self.state = CircuitState::Closed
        self.consecutive_failures = 0
        self.half_open_successes = 0
        self.in_flight_probes = 0
      }
    }
    Closed => self.consecutive_failures = 0
    Open => ()
  }
}

///|
pub fn CircuitBreaker::record_failure(self : CircuitBreaker) -> Unit {
  match self.state {
    HalfOpen => {
      self.state = CircuitState::Open
      self.opened_at_ms = self.clock.now_ms()
      self.half_open_successes = 0
      self.in_flight_probes = 0
    }
    Closed => {
      self.consecutive_failures = self.consecutive_failures + 1
      if self.consecutive_failures >= self.config.failure_threshold {
        self.state = CircuitState::Open
        self.opened_at_ms = self.clock.now_ms()
      }
    }
    Open => self.opened_at_ms = self.clock.now_ms()
  }
}

///|
pub async fn[T] CircuitBreaker::execute(
  self : CircuitBreaker,
  action : async () -> T,
) -> T {
  let (was_half_open, probe_epoch) = self.allow_request_tracked()
  let mut settled = false
  defer (if !settled &&
    was_half_open &&
    self.state == CircuitState::HalfOpen &&
    self.half_open_epoch == probe_epoch &&
    self.in_flight_probes > 0 {
    self.in_flight_probes = self.in_flight_probes - 1
  })
  let val = {
    // Do not trip the circuit breaker on caller task cancellation (client disconnect);
    // only release the half-open probe slot via `defer`.
    errdefer (if !@async.is_being_cancelled() {
      settled = true
      self.record_failure()
    })
    action()
  }
  settled = true
  self.record_success()
  val
}