#!/usr/bin/env python3.12
# -*- coding: utf-8 -*-
"""同步韩文文件：brand→GUCCI + focus_point 韩译"""
import json, re, urllib.request, yaml

key = None
try:
    with open('/root/.openclaw/openclaw.json') as f:
        key = json.load(f).get('api_key')
except Exception:
    pass
if not key:
    cfg = yaml.safe_load(open('/home/ubuntu/.hermes/config.yaml'))
    key = cfg['providers']['deepseek']['api_key']

DEEPSEEK_API_URL = 'https://api.deepseek.com/chat/completions'

def translate_fp(cn_fp):
    prompt = ('把以下中文策略启示翻译成韩文，保持30-45个韩文字符，语气柔和。'
              '品牌名用英文(中文)格式如 Dior(迪奥)、Cotti Coffee(库迪)、IM(智己)、Jimmy Choo(周仰杰)。'
              '只输出韩文翻译，不要多余内容。\n\n' + cn_fp)
    payload = json.dumps({
        "model": "deepseek-chat",
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.3,
        "max_tokens": 300
    }).encode()
    req = urllib.request.Request(DEEPSEEK_API_URL, data=payload,
        headers={'Authorization': f'Bearer {key}', 'Content-Type': 'application/json'})
    resp = urllib.request.urlopen(req, timeout=60)
    data = json.loads(resp.read().decode())
    return data['choices'][0]['message']['content'].strip()

# 中文最终文件的 focus_point（已更新）
fin = json.load(open('/data/news/json/0822data.json'))
new_fp = {}
for i, it in enumerate(fin.get('brand_hotspots', [])):
    new_fp[('brand_hotspots', i)] = it.get('focus_point', '')
new_fp[('vehicle_hotspots', 3)] = fin['vehicle_hotspots'][3].get('focus_point', '')

# 更新 ko 文件
ko = json.load(open('/data/news/json/ko-0822data.json'))
ko['brand_hotspots'][0]['brand'] = 'GUCCI'
print('✅ ko brand_hotspots[0].brand → GUCCI')

for (sec, idx), cn in new_fp.items():
    try:
        kr = translate_fp(cn)
        if sec in ko and idx < len(ko[sec]):
            ko[sec][idx]['focus_point'] = kr
            print(f'  ✅ ko {sec}[{idx}] → {kr}')
    except Exception as e:
        print(f'  ❌ ko {sec}[{idx}] 失败: {e}')

json.dump(ko, open('/data/news/json/ko-0822data.json', 'w'), ensure_ascii=False, indent=2)
print('\n✅ ko 文件已保存')
