#!/bin/bash
# 光湖视频AI系统 · 新模型连通性测试
# D144 · 铸渊 ICE-GL-ZY001 · 2026-06-24
# 测试: Seedance 1.5 + 声音复刻 2.0

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SYSTEM_DIR="$(dirname "$SCRIPT_DIR")"

# 加载环境变量
if [ -f "$SYSTEM_DIR/.env" ]; then
  set -a
  source "$SYSTEM_DIR/.env"
  set +a
fi

API_KEY="${JIMENG_API_KEY:-}"
BASE_URL="${JIMENG_BASE_URL:-https://ark.cn-beijing.volces.com/api/v3}"

if [ -z "$API_KEY" ]; then
  echo "❌ JIMENG_API_KEY 未设置"
  exit 1
fi

echo "══════════════════════════════════════════════"
echo "  光湖视频AI · 新模型连通性测试"
echo "  D144 · 铸渊 ICE-GL-ZY001"
echo "══════════════════════════════════════════════"
echo ""

# ─────────────────────────────────────
# 测试 1: 尝试通过 /api/v3 旧端点调用 1.5
# ─────────────────────────────────────
echo "【测试1】Seedance 1.5 通过 /api/v3/contents/generations/tasks"
echo "模型: doubao-seedance-1-5-pro-251215"
echo ""

RESP1=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/contents/generations/tasks" \
  -H "Authorization: Bearer ${API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "doubao-seedance-1-5-pro-251215",
    "content": [{"type": "text", "text": "一朵花在阳光下缓缓绽放，微风吹过花瓣"}],
    "duration": 5,
    "resolution": "480p"
  }' 2>&1)

HTTP_CODE1=$(echo "$RESP1" | tail -1)
BODY1=$(echo "$RESP1" | sed '$d')

echo "HTTP状态: $HTTP_CODE1"
echo "响应: $(echo "$BODY1" | python3 -c "import sys,json; d=json.load(sys.stdin); print(json.dumps(d,indent=2,ensure_ascii=False))" 2>/dev/null || echo "$BODY1")"
echo ""

# ─────────────────────────────────────
# 测试 2: 通过 /v1/video/generations 端点
# ─────────────────────────────────────
echo "【测试2】Seedance 1.5 通过 /v1/video/generations"
echo "模型: doubao-seedance-1-5-pro-251215"
echo ""

# 用 https://ark.cn-beijing.volces.com 作为基础
RESP2=$(curl -s -w "\n%{http_code}" -X POST "https://ark.cn-beijing.volces.com/v1/video/generations" \
  -H "Authorization: Bearer ${API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "doubao-seedance-1-5-pro-251215",
    "prompt": "一朵花在阳光下缓缓绽放，微风吹过花瓣",
    "resolution": "480p",
    "ratio": "16:9",
    "duration": 5,
    "fps": 24,
    "watermark": false,
    "draft": true,
    "generate_audio": true
  }' 2>&1)

HTTP_CODE2=$(echo "$RESP2" | tail -1)
BODY2=$(echo "$RESP2" | sed '$d')

echo "HTTP状态: $HTTP_CODE2"
echo "响应: $(echo "$BODY2" | python3 -c "import sys,json; d=json.load(sys.stdin); print(json.dumps(d,indent=2,ensure_ascii=False))" 2>/dev/null || echo "$BODY2")"
echo ""

# ─────────────────────────────────────
# 测试 3: 列出可用模型
# ─────────────────────────────────────
echo "【测试3】查询账号下可用模型列表"
echo ""

RESP3=$(curl -s -w "\n%{http_code}" -X GET "${BASE_URL}/models" \
  -H "Authorization: Bearer ${API_KEY}" \
  -H "Content-Type: application/json" 2>&1)

HTTP_CODE3=$(echo "$RESP3" | tail -1)
BODY3=$(echo "$RESP3" | sed '$d')

if [ "$HTTP_CODE3" = "200" ]; then
  echo "✅ 模型列表查询成功"
  echo "$BODY3" | python3 -c "
import sys, json
d = json.load(sys.stdin)
models = d.get('data', [])
print(f'账号共 {len(models)} 个模型:')
for m in models:
    mid = m.get('id', '?')
    mname = m.get('model', '?')
    print(f'  - {mid} (model={mname})')
" 2>/dev/null || echo "$BODY3"
else
  echo "HTTP状态: $HTTP_CODE3 (可能此端点不支持)"
fi

echo ""
echo "══════════════════════════════════════════════"
echo "  测试完成"
echo "══════════════════════════════════════════════"
