"""
简化版 Mock 数据插入脚本 - 只插入2条记录
"""
import asyncio
import httpx
import json
import uuid
import random

COZE_API_BASE = "https://api.coze.cn"
COZE_PAT_TOKEN = "pat_nd1wU47WyPS9GCIyJ1clnH8h1WOQXGrYELX8w73TnSZaYbFdYD4swIhzcETBUbfT"
WORKFLOW_ID = "7597376294612107318"

# 模拟候选人
CANDIDATES = [
    {"name": "张小美", "score": 85, "status": "completed", "risk": "low"},
    {"name": "李明辉", "score": 72, "status": "completed", "risk": "medium"},
]

# 简化对话
DIALOGUES = [
    {"stage": "专业技能", "q": "请介绍您的护肤专业知识", "a": "我在医美行业有3年经验..."},
    {"stage": "沟通能力", "q": "遇到客户投诉如何处理", "a": "首先我会认真倾听客户的问题..."},
]


async def execute_sql(sql: str, table: str) -> dict:
    headers = {
        "Authorization": f"Bearer {COZE_PAT_TOKEN}",
        "Content-Type": "application/json"
    }
    payload = {
        "workflow_id": WORKFLOW_ID,
        "parameters": {"input": json.dumps({"table": table, "sql": sql}, ensure_ascii=False)}
    }
    async with httpx.AsyncClient(timeout=30.0) as client:
        r = await client.post(f"{COZE_API_BASE}/v1/workflow/run", headers=headers, json=payload)
        return r.json()


async def main():
    print("=" * 50)
    print("插入 Mock 数据")
    print("=" * 50)
    
    for idx, c in enumerate(CANDIDATES, 1):
        session_id = f"MOCK_{uuid.uuid4().hex[:8].upper()}"
        
        # 生成评估报告
        report = f"""## 面试评估报告

**候选人**: {c['name']}
**综合评分**: {c['score']}/100

### 各维度评分
- 专业技能: {random.randint(70, 95)}分
- 沟通能力: {random.randint(70, 95)}分  
- 服务意识: {random.randint(70, 95)}分
- 学习能力: {random.randint(70, 95)}分

### 风险评估
风险等级: {c['risk']}

### 总结
该候选人整体表现{"良好" if c['score'] >= 80 else "一般"}，建议{"录用" if c['score'] >= 75 else "观察"}。
"""
        
        # 1. 插入评估记录
        sql1 = f"""INSERT INTO ci_interview_assessments (
            session_id, candidate_name, assessment_report, current_stage
        ) VALUES (
            '{session_id}', '{c["name"]}', '{report.replace("'", "''")}', '{c["status"]}'
        )"""
        
        print(f"\n📝 插入候选人 {idx}: {c['name']}")
        result = await execute_sql(sql1, "assessments")
        print(f"   评估记录: code={result.get('code')}")
        
        # 2. 插入对话记录
        for j, d in enumerate(DIALOGUES, 1):
            log_id = f"LOG_{session_id}_{j:02d}"
            sql2 = f"""INSERT INTO ci_interview_logs (
                log_id, session_id, stage, round, ai_question, user_answer, log_type
            ) VALUES (
                '{log_id}', '{session_id}', '{d["stage"]}', '{j}',
                '{d["q"].replace("'", "''")}', '{d["a"].replace("'", "''")}', 'interview'
            )"""
            
            result = await execute_sql(sql2, "logs")
            print(f"   对话{j}: code={result.get('code')}")
        
        await asyncio.sleep(0.5)
    
    print("\n" + "=" * 50)
    print("✅ 完成！")
    print("=" * 50)


if __name__ == "__main__":
    asyncio.run(main())
