"""
验证 extract_api_fields_from_fetch_root 能否从 FETCH_ROOT 正确提取 API 字段。
"""
import sys, os
from pathlib import Path
from dotenv import load_dotenv

load_dotenv(Path(__file__).resolve().parents[2] / ".env")

FETCH_ROOT = os.environ.get("FETCH_ROOT")
if not FETCH_ROOT:
    raise RuntimeError("FETCH_ROOT 未设置")

# 添加 ETL 模块路径
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "apps" / "etl" / "connectors" / "feiqiu"))

from quality.consistency_checker import (
    extract_api_fields_from_fetch_root,
    ODS_TABLE_TO_TASK_CODE,
    ODS_TABLE_TO_JSON_FILE,
)

fetch_root = Path(FETCH_ROOT)
print(f"FETCH_ROOT: {fetch_root}")
print(f"ODS 表数量: {len(ODS_TABLE_TO_JSON_FILE)}")
print(f"ODS→TaskCode 映射数量: {len(ODS_TABLE_TO_TASK_CODE)}")
print()

success = 0
skipped = 0
failed = 0

for ods_table in sorted(ODS_TABLE_TO_JSON_FILE.keys()):
    fields = extract_api_fields_from_fetch_root(fetch_root, ods_table)
    task_code = ODS_TABLE_TO_TASK_CODE.get(ods_table, "?")
    if fields is None:
        print(f"  [跳过] {ods_table} ({task_code}) — 无 JSON 数据")
        skipped += 1
    elif len(fields) == 0:
        print(f"  [失败] {ods_table} ({task_code}) — JSON 存在但提取到 0 个字段")
        failed += 1
    else:
        print(f"  [成功] {ods_table} ({task_code}) — {len(fields)} 个字段")
        success += 1

print(f"\n汇总: 成功={success}, 跳过={skipped}, 失败={failed}")
