///|
/// Max request body size accepted for buffering (64 MiB).
let max_body_size : Int32 = 64 * 1024 * 1024

///|
pub(all) suberror ProxyError {
  ProxyError(String)
}

///|
/// Handle one client connection: read one HTTP request, optionally rewrite
/// the Chat Completions system message, forward it to the upstream and
/// stream the response back (SSE works). The connection is closed
/// afterwards (`Connection: close`). Every proxied request is recorded
/// in the flow store for the monitor UI.
pub async fn handle_connection(
  client : @socket.Tcp,
  cfg : @config.Config,
  store : @monitor.FlowStore,
  client_addr : String,
) -> Unit {
  handle_connection_inner(client, cfg, store, client_addr) catch {
    e => println("[proxy] connection error: " + e.to_string())
  }
}

///|
async fn handle_connection_inner(
  client : @socket.Tcp,
  cfg : @config.Config,
  store : @monitor.FlowStore,
  client_addr : String,
) -> Unit {
  let line = match client.read_until("\r\n") {
    Some(l) if !l.is_empty() => l
    _ => return // client closed without sending anything
  }
  let (http_method, target, _version) = match
    @httpparse.parse_request_line(line) {
    Some(parts) => parts
    None => {
      send_simple(client, 400, "Bad Request")
      return
    }
  }
  if http_method == "CONNECT" {
    handle_connect(client, target, store, client_addr)
    return
  }
  let headers : Array[(String, String)] = []
  for ;; {
    match client.read_until("\r\n") {
      Some("") => break
      Some(l) =>
        match @httpparse.parse_header_line(l) {
          Some(h) => headers.push(h)
          None => ()
        }
      None => return // EOF in the middle of headers
    }
  }
  // Destination: in forward-proxy mode it is derived from the request itself
  // (absolute-form URL, or the Host header for origin-form targets), exactly
  // like mitmproxy; reverse mode keeps the pinned upstream.
  let (up_host, up_port, out_target) = if cfg.forward {
    if target.clamped_view().has_prefix("http://") {
      match @httpparse.absolute_http_target(target) {
        Some((h, p, path)) => (h, p, path)
        None => {
          send_simple(client, 400, "Bad Request")
          return
        }
      }
    } else if target.clamped_view().has_prefix("https://") {
      send_simple(client, 501, "HTTPS MITM Not Supported")
      return
    } else {
      match @httpparse.header_value({ http_method, target, headers, }, "host") {
        Some(hv) =>
          match @httpparse.parse_authority(hv, 80) {
            Some((h, p)) => (h, p, target)
            None => {
              send_simple(client, 400, "Bad Host Header")
              return
            }
          }
        None => {
          send_simple(client, 400, "Missing Host Header")
          return
        }
      }
    }
  } else {
    (cfg.upstream_host, cfg.upstream_port, target)
  }
  let head : @httpparse.RequestHead = {
    http_method,
    target: out_target,
    headers,
  }
  if @httpparse.header_value(head, "upgrade") is Some(_) {
    send_simple(client, 501, "Not Implemented")
    return
  }
  if @httpparse.header_value(head, "expect") is Some(v) &&
    v.clamped_view().to_lower().contains("100-continue") {
    client.write("HTTP/1.1 100 Continue\r\n\r\n")
  }
  let body = read_request_body(client, head)
  let intercepted = @httpparse.is_chat_completions(http_method, out_target)
  let upstream_addr = up_host + ":" + up_port.to_string()
  let flow = store.begin(
    client_addr~,
    http_method~,
    target~,
    request_headers=headers,
    request_body=body,
    intercepted~,
    upstream=upstream_addr,
  )
  // Finalize the flow even if the connection aborts mid-way.
  errdefer (if !flow.finished {
    if flow.error is None {
      flow.error = Some("connection aborted")
    }
    store.finish(flow)
  })
  let mut out_body = body
  if intercepted {
    if body.length() > 0 {
      try {
        let json = @json.parse(@utf8.decode(body))
        if ensure_system_prefix_with(json, cfg.system_prompt) {
          out_body = @utf8.encode(json.stringify())
          flow.injected = true
          let (capped, truncated) = @monitor.cap_bytes(out_body)
          flow.upstream_body = Some(capped)
          flow.upstream_truncated = truncated
          println(
            "[proxy] " +
            http_method +
            " " +
            target +
            " -> injected system prefix",
          )
        } else {
          println(
            "[proxy] " +
            http_method +
            " " +
            target +
            " -> system prefix ok, pass through",
          )
        }
      } catch {
        e =>
          println(
            "[proxy] warning: cannot parse body as JSON, forwarding unchanged (" +
            e.to_string() +
            ")",
          )
      }
    } else {
      println(
        "[proxy] " + http_method + " " + target + " -> empty body, pass through",
      )
    }
  }
  let upstream = @socket.Tcp::connect_to_host(up_host, port=up_port) catch {
    e => {
      println("[proxy] upstream connect failed: " + e.to_string())
      flow.error = Some("upstream connect failed: " + e.to_string())
      flow.status = 502
      flow.reason = "Bad Gateway"
      flow.response_body = @utf8.encode("proxy error: Bad Gateway\n")
      flow.response_size = flow.response_body.length()
      send_simple(client, 502, "Bad Gateway")
      store.finish(flow)
      return
    }
  }
  defer upstream.close()
  let send_cl = out_body.length() > 0 ||
    http_method == "POST" ||
    http_method == "PUT" ||
    http_method == "PATCH"
  let out_head = @httpparse.build_upstream_head(
    head,
    up_host,
    up_port,
    out_body.length(),
    send_cl,
  )
  upstream.write(out_head)
  if out_body.length() > 0 {
    upstream.write(out_body)
  }
  // Read the response head for the monitor, forward it verbatim to the
  // client, then stream the body back as it arrives (SSE / chunked),
  // capturing a capped copy for the monitor.
  let status_line = match upstream.read_until("\r\n") {
    Some(l) => l
    None => {
      flow.error = Some("upstream closed without response")
      store.finish(flow)
      return
    }
  }
  match @httpparse.parse_status_line(status_line) {
    Some((code, reason)) => {
      flow.status = code
      flow.reason = reason
    }
    None => flow.error = Some("bad status line: " + status_line)
  }
  let resp_headers : Array[(String, String)] = []
  let mut head_raw = status_line + "\r\n"
  for ;; {
    match upstream.read_until("\r\n") {
      Some("") => {
        head_raw = head_raw + "\r\n"
        break
      }
      Some(l) => {
        head_raw = head_raw + l + "\r\n"
        match @httpparse.parse_header_line(l) {
          Some(h) => resp_headers.push(h)
          None => ()
        }
      }
      None => break
    }
  }
  flow.response_headers = resp_headers
  client.write(head_raw)
  let resp_chunked = match find_header(resp_headers, "transfer-encoding") {
    Some(v) => v.clamped_view().to_lower().contains("chunked")
    None => false
  }
  let buf = @buffer.Buffer()
  let mut resp_size = 0
  for ;; {
    match upstream.read_some(max_len=65536) {
      Some(chunk) => {
        client.write(chunk)
        resp_size += chunk.length()
        if buf.length() < @monitor.body_capture_limit {
          let room = @monitor.body_capture_limit - buf.length()
          let take = if chunk.length() < room { chunk.length() } else { room }
          buf.write_bytes(chunk.clamped_view(end=take))
        }
      }
      None => break
    }
  }
  let raw = buf.to_bytes()
  flow.response_body = if resp_chunked {
    @monitor.decode_chunked_body(raw)
  } else {
    raw
  }
  flow.response_size = resp_size
  flow.response_truncated = resp_size > @monitor.body_capture_limit
  store.finish(flow)
}

///|
/// Byte counters for a blind CONNECT tunnel (client->upstream / reverse).
priv struct TunnelStats {
  mut up : Int32
  mut down : Int32
}

///|
/// Task handles so whichever pump ends first can cancel the other (a blocked
/// read is not guaranteed to wake when the socket is closed).
priv struct TunnelCtl {
  mut up_task : @async.Task[Unit]?
  mut down_task : @async.Task[Unit]?
}

///|
/// CONNECT: establish a blind TCP tunnel to the requested authority and pump
/// bytes both ways until either side closes. TLS traffic passes through
/// untouched (no MITM), so bodies are not inspectable; the monitor records
/// the connection and its byte counts.
async fn handle_connect(
  client : @socket.Tcp,
  target : String,
  store : @monitor.FlowStore,
  client_addr : String,
) -> Unit {
  let (host, port) = match @httpparse.parse_authority(target, 443) {
    Some(pair) => pair
    None => {
      send_simple(client, 400, "Bad CONNECT Target")
      return
    }
  }
  // Consume the CONNECT request headers before switching to raw pumping.
  let headers : Array[(String, String)] = []
  for ;; {
    match client.read_until("\r\n") {
      Some("") => break
      Some(l) =>
        match @httpparse.parse_header_line(l) {
          Some(h) => headers.push(h)
          None => ()
        }
      None => return
    }
  }
  let flow = store.begin(
    client_addr~,
    http_method="CONNECT",
    target~,
    request_headers=headers,
    request_body=@utf8.encode(""),
    intercepted=false,
    upstream=host + ":" + port.to_string(),
  )
  errdefer (if !flow.finished {
    if flow.error is None {
      flow.error = Some("connection aborted")
    }
    store.finish(flow)
  })
  let upstream = @socket.Tcp::connect_to_host(host, port~) catch {
    e => {
      flow.error = Some("upstream connect failed: " + e.to_string())
      flow.status = 502
      flow.reason = "Bad Gateway"
      send_simple(client, 502, "Bad Gateway")
      store.finish(flow)
      return
    }
  }
  defer upstream.close()
  client.write("HTTP/1.1 200 Connection Established\r\n\r\n")
  flow.status = 200
  flow.reason = "Connection Established"
  println("[proxy] CONNECT " + target + " -> blind tunnel (no MITM)")
  let stats : TunnelStats = { up: 0, down: 0, }
  let ctl : TunnelCtl = { up_task: None, down_task: None, }
  @async.with_task_group() <| group => {
    let t1 = group.spawn(allow_failure=true) <| () => {
      pump(client, upstream, stats, true)
      match ctl.down_task {
        Some(t) => t.cancel()
        None => ()
      }
    }
    let t2 = group.spawn(allow_failure=true) <| () => {
      pump(upstream, client, stats, false)
      match ctl.up_task {
        Some(t) => t.cancel()
        None => ()
      }
    }
    ctl.up_task = Some(t1)
    ctl.down_task = Some(t2)
  }
  flow.request_size = stats.up
  flow.response_size = stats.down
  client.close()
  upstream.close()
  store.finish(flow)
}

///|
/// Pump one direction of a tunnel. On EOF or write error just return: the
/// wrapper cancels the sibling pump (cancellation must happen while sockets
/// are still open, otherwise CancelIoEx cannot wake a blocked read), and the
/// sockets are closed once the whole group finishes.
async fn pump(
  src : @socket.Tcp,
  dst : @socket.Tcp,
  stats : TunnelStats,
  is_up : Bool,
) -> Unit {
  for ;; {
    match src.read_some(max_len=65536) {
      Some(chunk) => {
        // Count before writing: a cancellation landing on the write's
        // suspension point must not lose the byte count.
        if is_up {
          stats.up += chunk.length()
        } else {
          stats.down += chunk.length()
        }
        dst.write(chunk) catch {
          _ => break
        }
      }
      None => break
    }
  }
}

///|
fn find_header(headers : Array[(String, String)], name : String) -> String? {
  for pair in headers {
    if pair.0.clamped_view().equal_ignore_ascii_case(name) {
      return Some(pair.1)
    }
  }
  None
}

///|
/// Read the request body, supporting both `Content-Length` and
/// `Transfer-Encoding: chunked`.
async fn read_request_body(
  client : @socket.Tcp,
  head : @httpparse.RequestHead,
) -> Bytes {
  let chunked = match @httpparse.header_value(head, "transfer-encoding") {
    Some(v) => v.clamped_view().to_lower().contains("chunked")
    None => false
  }
  if chunked {
    let buf = @buffer.Buffer()
    for ;; {
      let size_line = match client.read_until("\r\n") {
        Some(l) => l
        None => raise ProxyError("EOF in chunked body")
      }
      let size_view = match size_line.clamped_view().split_once(";") {
        Some((s, _)) => s
        None => size_line.clamped_view()
      }
      let size = @string.parse_int(size_view.trim(), radix=16) catch {
        _ => raise ProxyError("bad chunk size: " + size_line)
      }
      if size < 0 || buf.length() + size > max_body_size {
        raise ProxyError("chunked body too large")
      }
      if size == 0 {
        // Consume trailers until the terminating empty line.
        for ;; {
          match client.read_until("\r\n") {
            Some("") | None => break
            _ => ()
          }
        }
        break
      }
      buf.write_bytes(client.read_exactly(size))
      let _ = client.read_exactly(2) // CRLF after each chunk
    }
    return buf.to_bytes()
  }
  match @httpparse.header_value(head, "content-length") {
    Some(v) => {
      let n = @string.parse_int(v.clamped_view().trim()) catch {
        _ => raise ProxyError("bad Content-Length: " + v)
      }
      if n < 0 || n > max_body_size {
        raise ProxyError("body too large or invalid Content-Length")
      }
      if n == 0 {
        @utf8.encode("")
      } else {
        client.read_exactly(n)
      }
    }
    None => @utf8.encode("")
  }
}

///|
async fn send_simple(
  client : @socket.Tcp,
  code : Int32,
  reason : String,
) -> Unit {
  let body = "proxy error: " + reason + "\n"
  let head = "HTTP/1.1 " +
    code.to_string() +
    " " +
    reason +
    "\r\n" +
    "Content-Type: text/plain; charset=utf-8\r\n" +
    "Content-Length: " +
    body.length().to_string() +
    "\r\n" +
    "Connection: close\r\n\r\n"
  client.write(head)
  client.write(body)
}