#!/usr/bin/env python3.12
# -*- coding: utf-8 -*-
"""1103 -> 1104 最终JSON重构：模板字段对齐 + 日期字段 + strategy_actions清空"""
import json, os
from datetime import datetime, timedelta

SRC = '/data/news/json/yes_data/1103data.json'
TPL = '/data/news/json/yes_data/final_jsontpl.json'
OUT = '/data/news/json/1104data.json'
IMG_DIR = '/data/news/images/1104'

src = json.load(open(SRC, encoding='utf-8'))
tpl = json.load(open(TPL, encoding='utf-8'))
src_map = {b['name']: b['list'] for b in src}

# 保留字段集合（终稿核心字段）
CORE = ['brand', 'model', 'title', 'summary', 'focus_point', 'platform',
        'source_url', 'publish_time', 'thumb', 'icon', 'heat_score']

out = {}
for k, v in tpl.items():
    out[k] = v

for sec in ['brand_hotspots', 'vehicle_hotspots', 'social_hotspots',
            'hyundai_buzz_topics_domestic', 'hyundai_buzz_topics_international']:
    tpl_keys = list(tpl[sec][0].keys())
    items = []
    for it in src_map.get(sec, []):
        clean = {}
        for key in tpl_keys:
            clean[key] = it.get(key, '' if key != 'heat_score' else 0)
        # 补非模板但属核心的字段
        for key in CORE:
            if key not in clean and it.get(key) not in (None, '', 0):
                clean[key] = it[key]
        if sec != 'social_hotspots' and 'heat_score' in clean and not clean['heat_score']:
            clean.pop('heat_score', None)
        items.append(clean)
    out[sec] = items

report_date = '2026-11-04'
yesterday = '2026-11-03'
out['report_date'] = report_date
out['data_time_range'] = f'{yesterday} 07:00 至 {report_date} 07:00'
out['hero_summary'] = ['', '', '']
# 1104 为周三 -> strategy_actions 清空
out['strategy_actions'] = []

json.dump(out, open(OUT, 'w', encoding='utf-8'), ensure_ascii=False, indent=2)

# 校验
total = 0
for sec in ['brand_hotspots', 'vehicle_hotspots', 'social_hotspots',
            'hyundai_buzz_topics_domestic', 'hyundai_buzz_topics_international']:
    lst = out[sec]
    total += len(lst)
    print(f'{sec}: {len(lst)}')
    for i, it in enumerate(lst):
        miss = []
        if not str(it.get('thumb', '')).strip():
            miss.append('thumb')
        if it.get('thumb', '').startswith('/images/'):
            p = it['thumb'].replace('/images/', '/data/news/images/')
            if not os.path.exists(p):
                miss.append('thumb文件缺失')
        if sec == 'social_hotspots' and not it.get('heat_score'):
            miss.append('heat_score')
        if sec.startswith('hyundai') and not it.get('icon'):
            miss.append('icon')
        if miss:
            print(f'   [{i}] {it.get("title","")[:20]} 缺: {miss}')
print('总计', total)
print('report_date', out['report_date'], '|', out['data_time_range'])
print('strategy_actions', out['strategy_actions'])
