#!/usr/bin/env python3
"""一次性脚本：检查 ETL 审计记录迁移状态 + 刷新项目级一览表。"""

import os
import shutil
from pathlib import Path

ROOT = Path(r"C:\Project\NeoZQYY")
ETL_AUDIT = ROOT / "apps" / "etl" / "pipelines" / "feiqiu" / "docs" / "audit" / "changes"
ROOT_AUDIT = ROOT / "docs" / "audit" / "changes"


def check_migration():
    """检查 ETL 审计记录是否全部迁移到根目录。"""
    if not ETL_AUDIT.is_dir():
        print(f"ETL 审计目录不存在: {ETL_AUDIT}")
        return

    etl_files = {f.name for f in ETL_AUDIT.iterdir() if f.suffix == ".md"}
    root_files = {f.name for f in ROOT_AUDIT.iterdir() if f.suffix == ".md"}

    missing = etl_files - root_files
    print(f"ETL: {len(etl_files)} 条, 根目录: {len(root_files)} 条")

    if missing:
        print(f"\n根目录缺失 {len(missing)} 条，正在复制:")
        for fname in sorted(missing):
            src = ETL_AUDIT / fname
            dst = ROOT_AUDIT / fname
            shutil.copy2(src, dst)
            print(f"  已复制: {fname}")
        print("迁移补全完成。")
    else:
        print("所有 ETL 审计记录已迁移到根目录，无需补充。")

    extra = root_files - etl_files
    if extra:
        print(f"\n根目录独有 {len(extra)} 条（monorepo 新增）:")
        for f in sorted(extra):
            print(f"  - {f}")


def refresh_dashboard():
    """刷新项目级审计一览表。"""
    import sys
    sys.path.insert(0, str(ROOT / "apps" / "etl" / "pipelines" / "feiqiu"))
    from scripts.gen_audit_dashboard import scan_audit_dir, render_dashboard

    entries = scan_audit_dir(ROOT_AUDIT)
    content = render_dashboard(entries)

    output = ROOT / "docs" / "audit" / "audit_dashboard.md"
    output.write_text(content, encoding="utf-8")
    print(f"\n已刷新一览表: {len(entries)} 条记录 → {output}")


if __name__ == "__main__":
    check_migration()
    refresh_dashboard()
