#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""工作台管道校验：客诉字段/离职归并、客户 enrich 生效。
用法: /usr/bin/python3 脚本/verify_pipeline_step.py <complaints|customers>
"""
import json
import sys
import collections
from pathlib import Path

BASE = Path(__file__).resolve().parent.parent
DATA = BASE / "数据"


def load(name):
    p = DATA / name
    return json.loads(p.read_text(encoding="utf-8")), p.stat().st_size


def complaints():
    d, size = load("mcp_complaints.json")
    items = d["items"] if isinstance(d, dict) and "items" in d else d
    n = len(items)
    print(f"file=mcp_complaints.json size={size/1024:.0f}KB n={n}")
    print("keys=", sorted(items[0].keys()))
    for f in ("creator", "area", "status", "created"):
        hit = sum(1 for i in items if i.get(f) not in (None, "", []))
        print(f"  {f}: {hit}/{n} = {hit/n*100:.1f}%")
    c = collections.Counter(i.get("creator") for i in items)
    print("离职归并检查:")
    for name in ("黄明月", "张立娅", "刘子研"):
        print(f"  {name}: {c.get(name, 0)}")
    print("top creators:", c.most_common(5))


def customers():
    d, size = load("mcp_customers.json")
    rows = d["customers"] if isinstance(d, dict) and "customers" in d else d
    n = len(rows)
    print(f"file=mcp_customers.json size={size/1024:.0f}KB n={n}")
    raw = sum(1 for r in rows if any(k.startswith("field_") for k in r))
    print(f"  仍含 field_* 未 enrich 行: {raw}")
    for f in ("group", "dept", "level", "ctype", "amount"):
        hit = sum(1 for r in rows if r.get(f) not in (None, "", []))
        print(f"  {f}: {hit}/{n} = {hit/n*100:.1f}%")
    unknown = sum(1 for r in rows if str(r.get("group")) in ("未知", "None", ""))
    print(f"  group=未知/空: {unknown} ({unknown/n*100:.1f}%)")
    amt = sum(1 for r in rows if isinstance(r.get("amount"), (int, float)) and r["amount"] > 0)
    print(f"  amount>0: {amt}")
    print("  sample keys:", sorted(rows[0].keys())[:20])


if __name__ == "__main__":
    which = sys.argv[1] if len(sys.argv) > 1 else "complaints"
    {"complaints": complaints, "customers": customers}[which]()
