"""
探路脚本 — 调 MiniMax embo-01，打印原始返回 JSON
确认向量字段位置和维度后再写正式 service。
"""

import httpx
import json
import os
from pathlib import Path

# 加载 .env
from dotenv import load_dotenv
_env_path = Path(__file__).parent.parent / ".env"
load_dotenv(str(_env_path))

api_key = os.environ.get("MINIMAX_EMBED_API_KEY", "")
group_id = os.environ.get("MINIMAX_GROUP_ID", "")

if not api_key or api_key == "your_api_key_here":
    print("[ERROR] MINIMAX_EMBED_API_KEY not configured, please edit backend/.env")
    exit(1)
if not group_id or group_id == "your_group_id_here":
    print("[ERROR] MINIMAX_GROUP_ID not configured, please edit backend/.env")
    exit(1)

print(f"API Key (first 4 chars): {api_key[:4]}...")
print(f"GroupId: {group_id}")
print()

# 最小调用
test_text = "这是一段测试文本，用于验证 embo-01 接口返回结构。"

print(f"Sending request, test text: {test_text}")
print("-" * 60)

try:
    resp = httpx.post(
        "https://api.minimax.chat/v1/embeddings",
        headers={
            "Authorization": f"Bearer {api_key}",
            "GroupId": group_id,
            "Content-Type": "application/json",
        },
        json={"model": "embo-01", "texts": [test_text], "type": "db"},
        timeout=30.0,
    )
    print(f"HTTP status: {resp.status_code}")
    print()
    data = resp.json()
    print(json.dumps(data, indent=2, ensure_ascii=False))

    # 提取向量，验证维度
    print()
    print("-" * 60)
    vectors = data.get("vectors", [])
    if vectors and len(vectors) > 0:
        embedding = vectors[0]
        dim = len(embedding)
        print(f"[OK] Embedding field: vectors[0]")
        print(f"[OK] Embedding dimension: {dim}")
        if dim != 1536:
            print(f"[STOP] Dimension is NOT 1536! Got {dim} - stopping here")
        else:
            print(f"[OK] Dimension correct: 1536")
            print(f"[OK] API call successful, structure confirmed.")
    else:
        print("[WARNING] vectors not found in response")

except Exception as e:
    print(f"[ERROR] Request failed: {e}")
