#!/usr/bin/env python3.12
"""阶段3 步骤8：字段清理 —— 只保留核心字段（空值键也保留），删 model/yesorno

用法: python3.12 clean_fields_generic.py <json文件>
"""
import json, sys

P = sys.argv[1]
d = json.load(open(P, encoding='utf-8'))

BASE = ['title', 'brand', 'summary', 'focus_point', 'thumb', 'source_url',
        'publish_time', 'platform']
SHAPE = {
    'brand_hotspots': BASE,
    'vehicle_hotspots': BASE,
    'social_hotspots': BASE + ['heat_score'],
    'hyundai_buzz_topics_domestic': BASE + ['icon'],
    'hyundai_buzz_topics_international': BASE + ['icon'],
}
DEFAULTS = {'icon': False, 'heat_score': 0}

n_fill, n_drop = 0, 0
for s, order in SHAPE.items():
    new_list = []
    for e in d.get(s, []):
        for k in order:
            if k not in e:
                e[k] = DEFAULTS.get(k, '')
                n_fill += 1
        for k in list(e.keys()):
            if k not in order:
                del e[k]
                n_drop += 1
        new_list.append({k: e[k] for k in order})
    d[s] = new_list

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

print(f'补回缺失核心字段: {n_fill} 处 | 删除非核心字段: {n_drop} 处')
for s in SHAPE:
    lst = d.get(s, [])
    keys = set()
    for e in lst: keys |= set(e.keys())
    print(f'  {s}: {len(lst)} 条 | {sorted(keys)}')
print('条数:', [len(d.get(s, [])) for s in SHAPE], '合计', sum(len(d.get(s, [])) for s in SHAPE))
print('icon 保留:', sum(1 for s in SHAPE for e in d.get(s, []) if 'icon' in e),
      '| heat_score 保留:', sum(1 for s in SHAPE for e in d.get(s, []) if 'heat_score' in e))
print('顶层键:', list(d.keys()))
