#!/usr/bin/env python3
"""Step 1-6 最终汇总核验"""
import json, os, time
from collections import Counter
D = '/Users/liuxinyuan/Desktop/Hermes输出-工作类/数据'
os.chdir(D)

def load(f):
    return json.load(open(f, encoding='utf-8'))

def mtime(f):
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(f)))

print('=== 产物文件 ===')
for f in ['mcp_complaints.json', 'mcp_customers.json', 'activities_feishu.json',
          'daily_activities_mcp.json', 'mcp_orders_input.json', 'workbench_data.json']:
    print('  %-28s %10d B  mtime=%s' % (f, os.path.getsize(f), mtime(f)))

c = load('mcp_complaints.json')
print('\n=== Step1 客诉 ===')
print('  total=%s updated=%s' % (c['total'], c['updated']))
cc = Counter(x['creator'] for x in c['items'])
print('  黄明月=%s 张立娅=%s 刘子研=%s' % (cc.get('黄明月'), cc.get('张立娅'), cc.get('刘子研')))
print('  by_urgency=%s' % c['by_urgency'])

cu = load('mcp_customers.json')
print('\n=== Step2 客户 ===')
print('  updated_at=%s count=%d' % (cu['updated_at'], len(cu['customers'])))

fa = load('activities_feishu.json')
print('\n=== Step3a 拜访视图 ===')
print('  months=%s total=%d' % (fa['months'], fa['total']))
print('  channel=%s' % fa['channel_breakdown'])

da = load('daily_activities_mcp.json')
def lab(v):
    if isinstance(v, dict): return v.get('label', '')
    if isinstance(v, list) and v: return ','.join(x.get('label','') if isinstance(x,dict) else str(x) for x in v)
    return str(v or '')
print('\n=== Step3b 日更活动 ===')
print('  updated=%s total=%d' % (da['updated'], da['total']))
print('  活动类型=%s' % dict(Counter(lab(x.get('field_76654e')) for x in da['items'])))
print('  月份=%s' % dict(Counter(str(x.get('start_time',''))[:7] for x in da['items'])))

o = load('mcp_orders_input.json')
print('\n=== Step4 订单 ===')
print('  条数=%d keys=%s' % (len(o), sorted(o[0].keys())[:6]))
print('  月份=%s' % dict(Counter(str(x.get('创建时间',''))[:7] for x in o)))

w = load('workbench_data.json')
print('\n=== Step5 合并 ===')
print('  _merged_at=%s size=%.1fMB keys=%d' % (w['_merged_at'], os.path.getsize('workbench_data.json')/1e6, len(w)))
print('  complaints=%d mcpCustomers=%d shipments=%d customers=%d myActivities=%s' % (
    len(w.get('complaints', {}).get('items', [])),
    len(w.get('mcpCustomers', {}).get('customers', [])),
    w.get('shipments', {}).get('total'),
    len(w.get('customers', [])),
    (w.get('myActivities', {}) or {}).get('updated')))
