# -*- coding: utf-8 -*-
"""Step 5 验收：workbench_data.json 合并时间 + 关键模块条数是否与源文件一致。"""
import json
import os
import time

B = os.path.dirname(os.path.abspath(__file__))


def n_of(path, getter):
    with open(os.path.join(B, path), encoding='utf-8') as f:
        try:
            return getter(json.load(f))
        except Exception as e:
            return 'ERR:%s' % e


w = json.load(open(os.path.join(B, 'workbench_data.json'), encoding='utf-8'))
print('_merged_at:', w.get('_merged_at'), '| 现在:', time.strftime('%Y-%m-%d %H:%M:%S'))
print('keys:', len(w.keys()), list(w.keys()))
print('size MB: %.2f' % (os.path.getsize(os.path.join(B, 'workbench_data.json')) / 1048576.0))

c = w.get('complaints') or {}
print('complaints.total:', c.get('total'), '| items:', len(c.get('items') or []), '| updated:', c.get('updated'))
mc = w.get('mcpCustomers') or {}
print('mcpCustomers.updated_at:', mc.get('updated_at'), '| n:', len(mc.get('customers') or []))
sh = w.get('shipments') or {}
print('shipments.total:', sh.get('total'), '| orders:', len(sh.get('orders') or []),
      '| h1:', sh.get('h1_count'), '| h2:', sh.get('h2_count'), '| 补漏:', sh.get('supplement_count'))

# 与源文件对账
src_c = n_of('mcp_complaints.json', lambda d: d.get('total'))
src_mc = n_of('mcp_customers.json', lambda d: len(d.get('customers') or []))
print('对账 客诉 源=%s vs 合并=%s → %s' % (src_c, c.get('total'), 'OK' if src_c == c.get('total') else '不一致'))
print('对账 客户 源=%s vs 合并=%s → %s' % (src_mc, len(mc.get('customers') or []), 'OK' if src_mc == len(mc.get('customers') or []) else '不一致'))
