#!/bin/bash
export LC_ALL=C
BODY='{"model":"mimo-v2.6-flash-free","messages":[{"role":"user","content":"hi"}],"stream":true,"max_tokens":16}'

run() { # $1=label, rest=curl args
  local label="$1"; shift
  printf '\n=== %s\n' "$label"
  curl -sS -o /tmp/sim_body -w '  HTTP %{http_code}  %{time_total}s\n' --max-time 40 "$@" 2>&1 | head -3
  printf '  body: '; head -c 260 /tmp/sim_body | tr -d '\n'; echo
}

echo "########## A) EXACT REPLICA (as 9router sends it) ##########"
run "direct, no auth" -X POST https://opencode.ai/zen/v1/chat/completions \
  -H 'Content-Type: application/json' -H 'Accept: text/event-stream' \
  -H 'x-opencode-client: desktop' -d "$BODY"

run "via proxy pool (172.19.0.1:20808)" -x http://172.19.0.1:20808 \
  -X POST https://opencode.ai/zen/v1/chat/completions \
  -H 'Content-Type: application/json' -H 'Accept: text/event-stream' \
  -H 'x-opencode-client: desktop' -d "$BODY"

echo
echo "########## B) WHICH HEADER MATTERS ##########"
run "without x-opencode-client" -X POST https://opencode.ai/zen/v1/chat/completions \
  -H 'Content-Type: application/json' -H 'Accept: text/event-stream' -d "$BODY"

run "with fake bearer token" -X POST https://opencode.ai/zen/v1/chat/completions \
  -H 'Content-Type: application/json' -H 'x-opencode-client: desktop' \
  -H 'Authorization: Bearer sk-invalid' -d "$BODY"

echo
echo "########## C) MODELS ENDPOINT ##########"
run "lists models" https://opencode.ai/zen/v1/models -H 'x-opencode-client: desktop'
