# 步骤⑦ storyboard 引擎: 豆包 AI 拆解剧本
import sys, json
sys.path.insert(0, '.')

from lib.doubao_chat import breakdown_script

with open('projects/deep-sea-voyage/EP01-SCRIPT-LOCK.hdlp', 'r', encoding='utf-8') as f:
    script = f.read()

print('发送豆包 AI 拆解剧本...')
result = breakdown_script(script, 1)

if 'error' in result:
    print('API ERROR:', json.dumps(result, ensure_ascii=False, indent=2))
elif 'content' in result:
    content = result['content']
    # 提取 JSON
    if '```json' in content:
        content = content.split('```json')[1].split('```')[0]
    elif '```' in content:
        content = content.split('```')[1].split('```')[0]
    
    # 保存到文件
    out_path = 'projects/deep-sea-voyage/STORYBOARD-AI.json'
    with open(out_path, 'w', encoding='utf-8') as f:
        f.write(content.strip())
    print('SAVED:', out_path)
    print('tokens:', result.get('tokens', 'N/A'))
    print('---')
    print(content.strip())
else:
    print('UNKNOWN:', result)
