///|
#derive(Debug, Eq)
pub(all) enum RateLimitAlgorithm {
  TokenBucket
  SlidingWindow
}

///|
#derive(Debug, Eq)
pub(all) struct RateLimitConfig {
  algorithm : RateLimitAlgorithm
  limit : Int32
  window_ms : Int64
  key_prefix : String
}

///|
pub fn RateLimitConfig::RateLimitConfig(
  algorithm~ : RateLimitAlgorithm = RateLimitAlgorithm::TokenBucket,
  limit~ : Int32 = 60,
  window_ms~ : Int64 = 60000L,
  key_prefix~ : String = "ciallo:rl:",
) -> RateLimitConfig {
  { algorithm, limit, window_ms, key_prefix, }
}

///|
#derive(Debug, Eq)
pub(all) struct RateLimitDecision {
  allowed : Bool
  limit : Int32
  remaining : Int32
  reset_at_ms : Int64
  retry_after_secs : Int32
}

///|
/// KvStore-backed Rate Limiter (Token Bucket & Sliding Window) with per-key in-process atomic serialization.
pub struct RateLimiter {
  kv : &@runtime.KvStore
  clock : &@runtime.Clock
  config : RateLimitConfig
  priv locks : Map[String, @async.Cond]
}

///|
pub fn RateLimiter::RateLimiter(
  kv~ : &@runtime.KvStore,
  clock~ : &@runtime.Clock = @runtime.SystemClock::SystemClock(),
  config~ : RateLimitConfig = RateLimitConfig::RateLimitConfig(),
) -> RateLimiter {
  { kv, clock, config, locks: Map([]), }
}

///|
pub async fn RateLimiter::check(
  self : RateLimiter,
  subject_key : StringView,
) -> RateLimitDecision {
  let limit = if self.config.limit < 1 { 1 } else { self.config.limit }
  let win = if self.config.window_ms < 1L {
    1000L
  } else {
    self.config.window_ms
  }
  let store_key = "\{self.config.key_prefix}\{subject_key}"
  while self.locks.get(store_key) is Some(cond) {
    cond.wait()
  }
  let my_cond = @async.Cond::Cond()
  self.locks[store_key] = my_cond
  defer {
    self.locks.remove(store_key)
    my_cond.broadcast()
  }
  let now = self.clock.now_ms()
  let ttl_sec = (win / 1000L + 2L).to_int()
  match self.config.algorithm {
    TokenBucket => {
      let mut tokens = limit.to_double()
      let mut last_ms = now
      match (self.kv.get(store_key) catch { _ => None }) {
        Some(raw) => {
          let parsed : Json? = Some(@json.parse(raw)) catch { _ => None }
          match parsed {
            Some(Object(m)) => {
              match m.get("tokens") {
                Some(Number(v, ..)) => tokens = v
                _ => ()
              }
              match m.get("last_ms") {
                Some(Number(v, ..)) => last_ms = v.to_int64()
                _ => ()
              }
            }
            _ => ()
          }
        }
        None => ()
      }
      let elapsed = if now > last_ms { now - last_ms } else { 0L }
      let refill = elapsed.to_double() * limit.to_double() / win.to_double()
      let cur = tokens + refill
      let capped = if cur > limit.to_double() { limit.to_double() } else { cur }
      if capped >= 1.0 {
        let next_tokens = capped - 1.0
        let payload = Json::object(
          Map([
            ("tokens", Json::number(next_tokens)),
            ("last_ms", Json::number(now.to_double())),
          ]),
        ).stringify()
        self.kv.put(store_key, payload, ttl_sec~) catch {
          _ => ()
        }
        {
          allowed: true,
          limit,
          remaining: next_tokens.to_int(),
          reset_at_ms: now + win,
          retry_after_secs: 0,
        }
      } else {
        let missing = 1.0 - capped
        let wait_ms = (missing * win.to_double() / limit.to_double()).to_int64()
        let retry_secs = ((wait_ms + 999L) / 1000L).to_int()
        {
          allowed: false,
          limit,
          remaining: 0,
          reset_at_ms: now + wait_ms,
          retry_after_secs: if retry_secs < 1 {
            1
          } else {
            retry_secs
          },
        }
      }
    }
    SlidingWindow => {
      let cutoff = now - win
      let timestamps : Array[Int64] = []
      match (self.kv.get(store_key) catch { _ => None }) {
        Some(raw) => {
          let parsed : Json? = Some(@json.parse(raw)) catch { _ => None }
          match parsed {
            Some(Array(arr)) =>
              for item in arr {
                match item {
                  Number(v, ..) => {
                    let ts = v.to_int64()
                    if ts > cutoff {
                      timestamps.push(ts)
                    }
                  }
                  _ => ()
                }
              }
            _ => ()
          }
        }
        None => ()
      }
      if timestamps.length() < limit {
        timestamps.push(now)
        let payload = Json::array(
          timestamps.map(t => Json::number(t.to_double())),
        ).stringify()
        self.kv.put(store_key, payload, ttl_sec~) catch {
          _ => ()
        }
        {
          allowed: true,
          limit,
          remaining: limit - timestamps.length(),
          reset_at_ms: now + win,
          retry_after_secs: 0,
        }
      } else {
        let oldest = timestamps[0]
        let wait_ms = if oldest + win > now {
          oldest + win - now
        } else {
          1000L
        }
        let retry_secs = ((wait_ms + 999L) / 1000L).to_int()
        {
          allowed: false,
          limit,
          remaining: 0,
          reset_at_ms: oldest + win,
          retry_after_secs: if retry_secs < 1 {
            1
          } else {
            retry_secs
          },
        }
      }
    }
  }
}

///|
/// Creates an EarlyHook enforcing RateLimiter with standard rate-limit headers and RFC 9457 429.
pub fn rate_limit_guard(
  limiter : RateLimiter,
  key_extractor~ : (@core.Context) -> String = ctx => {
    match ctx.get(@core.remote_addr_key) {
      Some(ip) => ip
      None => "global"
    }
  },
) -> @core.EarlyHook {
  ctx => {
    let key = key_extractor(ctx)
    let dec = limiter.check(key)
    ctx.set_header("x-ratelimit-limit", dec.limit.to_string())
    ctx.set_header("x-ratelimit-remaining", dec.remaining.to_string())
    ctx.set_header("x-ratelimit-reset", (dec.reset_at_ms / 1000L).to_string())
    if dec.allowed {
      None
    } else {
      let resp = @core.HttpError::too_many_requests("rate limit exceeded").to_response(
        trace_id=ctx.trace_id(),
      )
      resp.headers.set("retry-after", dec.retry_after_secs.to_string())
      resp.headers.set("x-ratelimit-limit", dec.limit.to_string())
      resp.headers.set("x-ratelimit-remaining", "0")
      Some(resp)
    }
  }
}

///|
/// Atomic Redis Lua Token Bucket script (refill + deduct + PEXPIRE in a single Redis round-trip).
/// KEYS[1] = bucket key; ARGV = [limit, window_ms, now_ms, cost]
/// Returns: [allowed (1|0), remaining, reset_at_ms, retry_after_secs]
pub let redis_token_bucket_lua : String =
  #|local key = KEYS[1]
  #|local limit = tonumber(ARGV[1])
  #|local win = tonumber(ARGV[2])
  #|local now = tonumber(ARGV[3])
  #|local cost = tonumber(ARGV[4]) or 1
  #|local cur = redis.call('HMGET', key, 'tokens', 'last_ms')
  #|local tokens = tonumber(cur[1])
  #|local last_ms = tonumber(cur[2])
  #|if not tokens or not last_ms then
  #|  tokens = limit
  #|  last_ms = now
  #|end
  #|local elapsed = math.max(0, now - last_ms)
  #|local refill = (elapsed * limit) / win
  #|tokens = math.min(limit, tokens + refill)
  #|if tokens >= cost then
  #|  tokens = tokens - cost
  #|  redis.call('HSET', key, 'tokens', tostring(tokens), 'last_ms', tostring(now))
  #|  redis.call('PEXPIRE', key, win + 2000)
  #|  return {1, math.floor(tokens), now + win, 0}
  #|else
  #|  local missing = cost - tokens
  #|  local wait_ms = math.ceil((missing * win) / limit)
  #|  local retry_secs = math.max(1, math.ceil(wait_ms / 1000))
  #|  return {0, 0, now + wait_ms, retry_secs}
  #|end

///|
/// Distributed atomic Redis Rate Limiter backed by `redis_token_bucket_lua` (`EVALSHA` / `EVAL`).
pub struct RedisRateLimiter {
  eval_lua : async (String, Array[String], Array[String]) -> Array[Int64]
  clock : &@runtime.Clock
  config : RateLimitConfig
  fail_open : Bool
}

///|
pub fn RedisRateLimiter::RedisRateLimiter(
  eval_lua~ : async (String, Array[String], Array[String]) -> Array[Int64],
  clock~ : &@runtime.Clock = @runtime.SystemClock::SystemClock(),
  config~ : RateLimitConfig = RateLimitConfig::RateLimitConfig(),
  fail_open~ : Bool = true,
) -> RedisRateLimiter {
  { eval_lua, clock, config, fail_open, }
}

///|
pub async fn RedisRateLimiter::check(
  self : RedisRateLimiter,
  subject_key : StringView,
) -> RateLimitDecision {
  let now = self.clock.now_ms()
  let limit = if self.config.limit < 1 { 1 } else { self.config.limit }
  let win = if self.config.window_ms < 1L {
    1000L
  } else {
    self.config.window_ms
  }
  let store_key = "\{self.config.key_prefix}\{subject_key}"
  let fallback = if self.fail_open {
    [1L, (limit - 1).to_int64(), now + win, 0L]
  } else {
    [0L, 0L, now + win, 1L]
  }
  let out = (self.eval_lua)(redis_token_bucket_lua, [store_key], [
    limit.to_string(),
    win.to_string(),
    now.to_string(),
    "1",
  ]) catch {
    _ => fallback
  }
  if out.length() >= 4 {
    {
      allowed: out[0] != 0L,
      limit,
      remaining: out[1].to_int(),
      reset_at_ms: out[2],
      retry_after_secs: out[3].to_int(),
    }
  } else {
    {
      allowed: self.fail_open,
      limit,
      remaining: if self.fail_open {
        limit - 1
      } else {
        0
      },
      reset_at_ms: now + win,
      retry_after_secs: if self.fail_open {
        0
      } else {
        1
      },
    }
  }
}

///|
/// Creates an EarlyHook enforcing `RedisRateLimiter` with standard rate-limit headers and RFC 9457 429.
pub fn redis_rate_limit_guard(
  limiter : RedisRateLimiter,
  key_extractor~ : (@core.Context) -> String = ctx => {
    match ctx.get(@core.remote_addr_key) {
      Some(ip) => ip
      None => "global"
    }
  },
) -> @core.EarlyHook {
  ctx => {
    let key = key_extractor(ctx)
    let dec = limiter.check(key)
    ctx.set_header("x-ratelimit-limit", dec.limit.to_string())
    ctx.set_header("x-ratelimit-remaining", dec.remaining.to_string())
    ctx.set_header("x-ratelimit-reset", (dec.reset_at_ms / 1000L).to_string())
    if dec.allowed {
      None
    } else {
      let resp = @core.HttpError::too_many_requests("rate limit exceeded").to_response(
        trace_id=ctx.trace_id(),
      )
      resp.headers.set("retry-after", dec.retry_after_secs.to_string())
      resp.headers.set("x-ratelimit-limit", dec.limit.to_string())
      resp.headers.set("x-ratelimit-remaining", "0")
      Some(resp)
    }
  }
}