"""完整评分流水线：Stage 1 + Stage 2 DeepSeek 协议评分
品牌全部、车型前15（含车型检测）、社会前30
输出到新文件，不污染原始数据
"""

import json, os, re, urllib.request, copy
from datetime import datetime

DEEPSEEK_API_URL = 'https://api.deepseek.com/v1/chat/completions'
INPUT = '/data/news/json/origin_data/0616data.json'
OUTPUT = '/data/news/json/origin_data/yes_0616data.json'

# ---- 关键词维度 ----
KEYWORDS_ALL = {
    '体育营销': ['世界杯', '奥运', '体育', '赛事', '运动员', '欧冠', '决赛', '球迷', '观赛', '比赛', '球场'],
    '情感营销': ['情感', '共鸣', '温度', '故事', '怀旧', '情怀', '温情', '暖心', '感动', '治愈'],
    '用户运营': ['用户', '私域', '社群', '会员', '粉丝', '圈层', '忠诚', '车主', '社区', '互动', '打卡'],
    '线下体验': ['体验', '场景', '快闪', '线下', '门店', '沉浸', '试驾', '到店', '工厂', '探访'],
    '本土化': ['本土化', '本土', '中国风', '国潮', '传统', '文化', '非遗', '国货', '中国'],
    '联名合作': ['联名', '跨界', 'IP', '联乘', '合作款', '限定', '定制', '联名款', '携手'],
    'AI/数字化': ['AI', '人工智能', '数字化', '数据', '算力', '算法', '大模型', '智能系统', '芯片', '半导体'],
    '明星代言': ['代言', '明星', 'KOL', '艺人', '大使', '官宣', '明星营销'],
    '内容营销': ['短剧', '内容', '短视频', '直播', '共创', 'UGC', '短片', '视频', '广告', '种草'],
    '新车': ['上市', '发布', '预售', '亮相', '首发', '开启预售', '新车'],
    '电动化': ['电动', '纯电', 'EV', '续航', '充电', '新能源', '纯电动'],
    '混动': ['混动', 'PHEV', '增程', '轻混', '混动版', '插混'],
    '智能化': ['智能', '智驾', '自动驾驶', '座舱', '激光雷达', '乾崑', '天枢'],
    '价格/权益': ['售价', '万元', '万起', '补贴', '优惠', '限时', '福利', '降价', '置换', '金融', '低至'],
    '消费/经济': ['消费', '经济', '就业', '收入', '物价', '房价', '补贴', '政策'],
    '出行/交通': ['出行', '交通', '自驾', '旅游', '通勤', '航空', '高铁', '地铁'],
}

BRAND_WEIGHTS = {'体育营销':0.70,'情感营销':0.68,'用户运营':0.73,'线下体验':0.56,'本土化':0.53,'联名合作':0.46,'AI/数字化':0.39,'明星代言':0.33,'内容营销':0.21,'价格/权益':0.15}
BRAND_TOTAL = sum(BRAND_WEIGHTS.values())

VEHICLE_WEIGHTS = {'用户运营':0.71,'线下体验':0.44,'本土化':0.59,'AI/数字化':0.28,'内容营销':0.15,'新车':0.99,'电动化':0.74,'混动':0.38,'智能化':0.55,'价格/权益':0.65}
VEHICLE_TOTAL = sum(VEHICLE_WEIGHTS.values())

SOCIAL_WEIGHTS = {'体育营销':0.39,'用户运营':0.10,'线下体验':0.10,'本土化':0.10,'AI/数字化':0.20,'智能化':0.10,'价格/权益':0.08,'消费/经济':0.30,'出行/交通':0.25}
SOCIAL_TOTAL = sum(SOCIAL_WEIGHTS.values())

# ---- 车型检测 ----
def has_vehicle_model(item):
    model = (item.get('model') or '').strip()
    if model:
        return True
    title = item.get('title', '')
    patterns = [r'[A-Z][0-9]', r'[A-Za-z]+\s*EV', r'[0-9]+款\s*[^\s]+',
                r'猎手K[0-9]+', r'钛7|泰山X8|豪越L|星途ES|凡尔赛C5|星光L|悦意08|欧拉7|MR2|银河战舰']
    for p in patterns:
        if re.search(p, title):
            return True
    return False

# ---- DeepSeek Key ----
def get_ds_key():
    try:
        with open('/root/.openclaw/openclaw.json') as f:
            return json.load(f)['models']['providers']['deepseek']['apiKey']
    except:
        return None

# ---- Stage 1 评分 ----
def stage1(text, weights, total):
    text_lower = text.lower()
    hit_w = 0.0
    hit_n = 0
    for dim, keywords in KEYWORDS_ALL.items():
        w = weights.get(dim, 0)
        for kw in keywords:
            if kw.lower() in text_lower:
                hit_w += w
                hit_n += 1
                break
    raw = (hit_w / total) * 100 if total else 0
    bonus = min(hit_n * 5, 20)
    s = min(100, raw + bonus)
    now = datetime.now()
    if (now.month == 6 and now.day >= 12) or (now.month == 7 and now.day <= 20):
        for kw in ['世界杯', '赛事', '观赛', '球迷', '足球']:
            if kw.lower() in text_lower:
                s = min(100, s * 1.4)
                break
    return s

# ---- DeepSeek 批次调用 ----
def call_ds(items, section_name):
    key = get_ds_key()
    if not key:
        return None
    lines = []
    for i, it in enumerate(items, 1):
        lines.append("[%d] 标题：%s" % (i, it.get('title', '')))
        lines.append("    摘要：%s" % it.get('summary', ''))
    items_text = "\n".join(lines)

    guide = {
        'brand_hotspots': (
            "评分标准（0-100）：\n"
            "90-100：与汽车/出行行业高度相关的品牌营销案例\n"
            "70-89：有价值的品牌营销动作，对现代汽车有参考意义\n"
            "50-69：普通品牌动态\n"
            "0-49：与汽车行业无关或价值较低"),
        'vehicle_hotspots': (
            "评分标准（0-100）：\n"
            "90-100：重磅新车上市/预售\n"
            "70-89：重要车型动态\n"
            "50-69：普通车型信息\n"
            "0-49：与车型/汽车行业无关"),
        'social_hotspots': (
            "评分标准（0-100）：\n"
            "90-100：与汽车消费/出行相关的全网热点\n"
            "70-89：有影响力的社会话题\n"
            "50-69：普通社会新闻\n"
            "0-49：低价值或无关")
    }

    # 新增规则
    new_rules = (
        "\n\n## 新增规则（优先级高）\n"
        "1. ❌ 综合分析文章意图——判断是客观新闻报道还是主观推广PR/软文。考虑整体语气、立场、信息来源，而非止于标题关键词。客观报道（即便含正面描述）可保留，明确PR/软文/通稿倾向的降档。\n"
        "2. ❌ 减少疑问句/设问标题文章的权重：'做对了什么''为什么''如何''怎么''吗？'等提问方式的标题，多为观点文而非事实新闻，自动降档。\n"
    )

    prompt = ("你是现代汽车营销情报分析师。请为以下%d条%s逐条判定综合价值评分。\n%s%s\n\n"
              "评分映射：\n"
              "- ≥ 65: strong_select → yes\n"
              "- 45-64: select → yes\n"
              "- 25-44: backup\n"
              "- < 25: reject\n\n"
              "输出格式（严格JSON数组，不要其他文字）：\n"
              "[\n"
              "  {\"idx\": 1, \"selection_suggestion\": \"select\", \"scores\": {\"overall_tag_value_score\": 0-100}, \"selection_reason\": \"15字内理由\", \"marketing_opportunity\": \"20字内启示\", \"event_type\": \"\", \"business_tags\": [], \"risk_tags\": [\"无明显风险\"]},\n"
              "  ...\n"
              "]\n\n待评数据：\n%s"
    ) % (len(items), section_name, guide.get(section_name, ''), new_rules, items_text)

    payload = json.dumps({
        "model": "deepseek-chat",
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.3,
        "max_tokens": 3000
    }).encode()

    req = urllib.request.Request(DEEPSEEK_API_URL, data=payload,
        headers={'Authorization': 'Bearer ' + key, 'Content-Type': 'application/json'})
    try:
        resp = urllib.request.urlopen(req, timeout=90)
        reply = json.loads(resp.read().decode('utf-8'))['choices'][0]['message']['content'].strip()
        json_start = reply.find('[')
        json_end = reply.rfind(']') + 1
        if json_start >= 0 and json_end > json_start:
            return json.loads(reply[json_start:json_end])
        print("  ⚠️ 解析失败: %s" % reply[:150])
        return None
    except Exception as e:
        print("  ❌ DeepSeek失败: %s" % e)
        return None


def main():
    print("=" * 60)
    print("完整评分流水线")
    print("=" * 60)
    
    with open(INPUT, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 深拷贝避免污染源文件
    out = copy.deepcopy(data)
    
    for section in out:
        name = section.get('name', '')
        items = section.get('list', [])
        if not items:
            continue
        
        print("\n【%s】共%d条" % (name, len(items)))
        
        if name == 'brand_hotspots':
            # 全部进DeepSeek
            results = call_ds(items, name)
            if results:
                for idx, it in enumerate(items):
                    r = results[idx] if idx < len(results) else None
                    if not r:
                        continue
                    overall = r.get('scores', {}).get('overall_tag_value_score', 50)
                    it['yesorno'] = 'yes' if overall >= 45 else ('backup' if overall >= 25 else '')
            
            counts = {'yes': sum(1 for it in items if it.get('yesorno') == 'yes'),
                      'backup': sum(1 for it in items if it.get('yesorno') == 'backup')}
            print("  yes=%d backup=%d" % (counts['yes'], counts['backup']))
        
        elif name == 'vehicle_hotspots':
            # Stage 1: 有车型的排前
            scored = [(stage1(it.get('title','')+' '+it.get('summary',''), VEHICLE_WEIGHTS, VEHICLE_TOTAL), has_vehicle_model(it), it) for it in items]
            scored.sort(key=lambda x: (x[1], x[0]), reverse=True)
            
            top15 = [x[2] for x in scored if x[1]][:15]
            rest_ids = {id(it) for it in items} - {id(it) for it in top15}
            
            for it in items:
                if id(it) not in {id(t) for t in top15}:
                    it['yesorno'] = ''
            
            results = call_ds(top15, name)
            if results:
                for idx, it in enumerate(top15):
                    r = results[idx] if idx < len(results) else None
                    if not r:
                        continue
                    overall = r.get('scores', {}).get('overall_tag_value_score', 50)
                    it['yesorno'] = 'yes' if overall >= 45 else ('backup' if overall >= 25 else '')
            
            counts = {'yes': sum(1 for it in items if it.get('yesorno') == 'yes'),
                      'backup': sum(1 for it in items if it.get('yesorno') == 'backup')}
            print("  yes=%d backup=%d" % (counts['yes'], counts['backup']))
        
        elif name == 'social_hotspots':
            # Stage 1: 关键词评分取前30
            scored = [(stage1(it.get('title','')+' '+it.get('summary',''), SOCIAL_WEIGHTS, SOCIAL_TOTAL), it) for it in items]
            scored.sort(key=lambda x: x[0], reverse=True)
            top30 = [it for _, it in scored[:30]]
            
            for it in items:
                it['yesorno'] = ''
            
            results = call_ds(top30, name)
            if results:
                for idx, it in enumerate(top30):
                    r = results[idx] if idx < len(results) else None
                    if not r:
                        continue
                    overall = r.get('scores', {}).get('overall_tag_value_score', 50)
                    it['yesorno'] = 'yes' if overall >= 45 else ('backup' if overall >= 25 else '')
            
            # 按overall排序取top 10
            scored_items = []
            for it in items:
                scores = it.get('protocol_scores', {})
                overall = scores.get('overall_tag_value_score', 0)
                if it.get('yesorno') in ('yes', 'backup'):
                    scored_items.append((overall, it))
            scored_items.sort(key=lambda x: x[0], reverse=True)
            
            # 只保留前10条为yes，其余清空
            for idx, (_, it) in enumerate(scored_items):
                if idx < 10:
                    it['yesorno'] = 'yes'
                else:
                    it['yesorno'] = ''
            
            counts = {'yes': sum(1 for it in items if it.get('yesorno') == 'yes')}
            print("  yes=%d" % counts['yes'])
        
        elif name.startswith('hyundai'):
            # 现代板块暂不评分，保持原样
            pass
    
    # 清理协议字段（只保留yesorno和必要字段）
    clean_fields = ['protocol_scores', 'selection_suggestion', 'selection_reason', 'marketing_opportunity', 'event_type', 'business_tags', 'risk_tags']
    for section in out:
        for it in section.get('list', []):
            for f in clean_fields:
                it.pop(f, None)
    
    # 写入输出文件
    os.makedirs(os.path.dirname(OUTPUT), exist_ok=True)
    with open(OUTPUT, 'w', encoding='utf-8') as f:
        json.dump(out, f, ensure_ascii=False, indent=2)
    
    print("\n" + "=" * 60)
    print("✅ 已保存: %s" % OUTPUT)
    print("=" * 60)


if __name__ == '__main__':
    main()
