"""
一次性脚本：将已被 docs/database/ddl/ 覆盖的旧 DDL 文件归档到 db/_archived/。
迁移脚本、种子数据、FDW 配置、工具脚本不动。

用法：cd C:\\NeoZQYY && python scripts/ops/_archive_old_ddl.py
"""
import shutil
from pathlib import Path
from datetime import date

ROOT = Path(__file__).resolve().parent.parent.parent

ARCHIVE_DIR = ROOT / "db" / "_archived" / f"ddl_baseline_{date.today().isoformat()}"

# 需要归档的文件：旧基线 DDL（已被 docs/database/ddl/ 完全覆盖）
FILES_TO_ARCHIVE = [
    # etl_feiqiu/schemas/ 下的当前基线
    "db/etl_feiqiu/schemas/meta.sql",
    "db/etl_feiqiu/schemas/ods.sql",
    "db/etl_feiqiu/schemas/dwd.sql",
    "db/etl_feiqiu/schemas/core.sql",
    "db/etl_feiqiu/schemas/dws.sql",
    "db/etl_feiqiu/schemas/app.sql",
    # etl_feiqiu/schemas/ 下的历史遗留版本（使用旧 schema 名 billiards_*/etl_admin）
    "db/etl_feiqiu/schemas/schema_dwd_doc.sql",
    "db/etl_feiqiu/schemas/schema_dws.sql",
    "db/etl_feiqiu/schemas/schema_etl_admin.sql",
    "db/etl_feiqiu/schemas/schema_ODS_doc.sql",
    "db/etl_feiqiu/schemas/schema_verify_perf_indexes.sql",
    # zqyy_app/schemas/ 下的基线
    "db/zqyy_app/schemas/init.sql",
]


def main():
    ARCHIVE_DIR.mkdir(parents=True, exist_ok=True)

    moved = []
    skipped = []

    for rel in FILES_TO_ARCHIVE:
        src = ROOT / rel
        if not src.exists():
            skipped.append(rel)
            continue

        # 保留原始目录结构
        dest = ARCHIVE_DIR / rel
        dest.parent.mkdir(parents=True, exist_ok=True)
        shutil.move(str(src), str(dest))
        moved.append(rel)

    print(f"归档目录：{ARCHIVE_DIR}")
    print(f"已移动 {len(moved)} 个文件：")
    for f in moved:
        print(f"  ✅ {f}")
    if skipped:
        print(f"跳过 {len(skipped)} 个（不存在）：")
        for f in skipped:
            print(f"  ⏭️  {f}")

    # 保留 .gitkeep
    for d in ["db/etl_feiqiu/schemas", "db/zqyy_app/schemas"]:
        gk = ROOT / d / ".gitkeep"
        if not gk.exists():
            gk.touch()
            print(f"  📄 补充 {d}/.gitkeep")

    print(f"\n✅ 完成。旧 DDL 已归档，schemas/ 目录保留 .gitkeep")


if __name__ == "__main__":
    main()
