#!/bin/bash
# omarchy:summary=Test connectivity, auth, and model availability for an AI profile
# omarchy:args=[profile]
# omarchy:examples=omarchycn ai test | omarchycn ai test work

set -euo pipefail

source "$OMARCHY_PATH/cn/lib/ai.sh"

profile="${1:-$(cn_ai_current_profile)}"

if [[ ! -f $CN_AI_PROFILE_DIR/$profile.toml ]]; then
  echo "No such profile: $profile" >&2
  exit 1
fi

harness=$(cn_ai_profile_field "$profile" harness)
provider=$(cn_ai_profile_field "$profile" provider)
model=$(cn_ai_profile_field "$profile" model)
proto=$(cn_ai_harness_field "$harness" protocol)
# Codex speaks the Responses API (wire_api=responses), not chat/completions
[[ $harness == "codex" ]] && proto="openai-responses"
failures=0

if [[ $proto == "native" ]]; then
  echo "INFO ai-test: $harness 自管鉴权，本测试仅覆盖 env/config 型 Harness"
  exit 0
fi

# Endpoint override for mock-server tests
base="${OMARCHYCN_AI_TEST_BASE_URL:-$(cn_ai_endpoint "$provider" "${proto%-responses}")}"
echo "INFO ai-test: $profile ($harness x $provider x $model) -> $base"

if ! key=$(omarchy-cn-ai-secret get "$provider" 2>/dev/null); then
  echo "FAIL ai-test: 未存储 $provider 的 API Key (omarchycn ai secret set $provider)"
  exit 1
fi

body=$(mktemp)
trap 'rm -f "$body"' EXIT

case "$proto" in
anthropic)
  code=$(curl -sS -o "$body" -w '%{http_code}' -m 30 --connect-timeout 8 \
    -X POST "${base%/}/v1/messages" \
    -H "x-api-key: $key" -H "authorization: Bearer $key" \
    -H "anthropic-version: 2023-06-01" -H "content-type: application/json" \
    -d "{\"model\":\"$model\",\"max_tokens\":8,\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}" || true)
  ;;
openai)
  code=$(curl -sS -o "$body" -w '%{http_code}' -m 30 --connect-timeout 8 \
    -X POST "${base%/}/chat/completions" \
    -H "Authorization: Bearer $key" -H "content-type: application/json" \
    -d "{\"model\":\"$model\",\"max_tokens\":8,\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}" || true)
  ;;
openai-responses)
  code=$(curl -sS -o "$body" -w '%{http_code}' -m 30 --connect-timeout 8 \
    -X POST "${base%/}/responses" \
    -H "Authorization: Bearer $key" -H "content-type: application/json" \
    -d "{\"model\":\"$model\",\"input\":\"ping\",\"max_output_tokens\":16}" || true)
  ;;
*)
  echo "FAIL ai-test: unknown protocol $proto"
  exit 1
  ;;
esac
[[ -z $code ]] && code=000

case "$code" in
200)
  echo "PASS ai-test: 连接、鉴权与模型调用成功 (HTTP 200)"
  ;;
401 | 403)
  echo "FAIL ai-test: 鉴权失败 (HTTP $code)，检查 API Key"
  failures=$((failures + 1))
  ;;
404 | 400 | 422)
  echo "FAIL ai-test: 端点可达但请求被拒 (HTTP $code)，可能是模型名问题"
  head -c 200 "$body" 2>/dev/null && echo
  failures=$((failures + 1))
  ;;
000)
  echo "FAIL ai-test: 无法连接 $base (DNS/TLS/网络)"
  failures=$((failures + 1))
  ;;
*)
  echo "FAIL ai-test: HTTP $code"
  head -c 200 "$body" 2>/dev/null && echo
  failures=$((failures + 1))
  ;;
esac

rm -f $body
exit $((failures > 0 ? 1 : 0))
