"""
一次性脚本：将 docs/database/ 下的 ETL 专属 BD_Manual 文件迁移到
apps/etl/connectors/feiqiu/docs/database/ 对应子目录，
将迁移变更记录归档到 docs/database/_archived/。
"""
import shutil
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent.parent
SRC = ROOT / "docs" / "database"
ETL_DB = ROOT / "apps" / "etl" / "connectors" / "feiqiu" / "docs" / "database"
ARCHIVED = SRC / "_archived"

# 新建 cross_layer 目录（ODS→DWD 跨层映射文档）
CROSS_LAYER = ETL_DB / "cross_layer"
CROSS_LAYER.mkdir(exist_ok=True)

# ── A. ODS→DWD 跨层映射 → ETL cross_layer/ ──
cross_layer_files = [
    "BD_Manual_assistant_accounts_master.md",
    "BD_Manual_assistant_service_records.md",
    "BD_Manual_goods_stock_movements.md",
    "BD_Manual_goods_stock_summary.md",
    "BD_Manual_member_balance_changes.md",
    "BD_Manual_recharge_settlements.md",
    "BD_Manual_site_tables_master.md",
    "BD_Manual_store_goods_master.md",
    "BD_Manual_store_goods_sales_records.md",
    "BD_Manual_tenant_goods_master.md",
    "BD_Manual_group_buy_package_details.md",
    "BD_Manual_goods_stock_warning_info.md",  # ODS→DWD 加列变更
]

# ── B. DWD/DWS 层文档 → ETL 对应子目录 ──
dwd_files = [
    "BD_Manual_dim_groupbuy_package_ex_detail_fields.md",
]
dws_files = [
    "BD_Manual_dws_goods_stock_summary.md",
    "BD_Manual_dws_project_tags.md",
    "BD_Manual_dws_assistant_order_contribution.md",
    "BD_Manual_dws_member_spending_power_index.md",
]

# ── C. 迁移变更记录 → _archived/ ──
archive_files = [
    "BD_Manual_20260301_cleanup_and_fixes.md",
    "BD_Manual_biz_date_function_and_mv_rebuild.md",
    "BD_Manual_fix_dim_staff_ex_rankname.md",
    "BD_Manual_fix_dws_assistant_daily_table_area.md",
    "BD_Manual_tenant_id_int_to_bigint.md",
]

moved = []
archived = []
skipped = []

def move_file(src_path: Path, dst_path: Path, label: str):
    if not src_path.exists():
        skipped.append(f"[跳过] {src_path.name} — 文件不存在")
        return
    dst_path.parent.mkdir(parents=True, exist_ok=True)
    shutil.move(str(src_path), str(dst_path))
    return True

# A: cross_layer
for f in cross_layer_files:
    src = SRC / f
    dst = CROSS_LAYER / f
    if move_file(src, dst, "cross_layer"):
        moved.append(f"  {f} → cross_layer/")

# B: DWD
for f in dwd_files:
    src = SRC / f
    dst = ETL_DB / "DWD" / "changes" / f
    if move_file(src, dst, "DWD/changes"):
        moved.append(f"  {f} → DWD/changes/")

for f in dws_files:
    src = SRC / f
    dst = ETL_DB / "DWS" / "changes" / f
    if move_file(src, dst, "DWS/changes"):
        moved.append(f"  {f} → DWS/changes/")

# C: archive
for f in archive_files:
    src = SRC / f
    dst = ARCHIVED / f
    if move_file(src, dst, "_archived"):
        archived.append(f"  {f}")

print("=== BD_Manual 文档整理完成 ===\n")
print(f"迁移到 ETL 模块 ({len(moved)} 个):")
for m in moved:
    print(m)
print(f"\n归档到 _archived/ ({len(archived)} 个):")
for a in archived:
    print(a)
if skipped:
    print(f"\n跳过 ({len(skipped)} 个):")
    for s in skipped:
        print(s)
print(f"\n保留在 docs/database/ 的文件:")
remaining = [p.name for p in SRC.glob("BD_Manual_*.md") if p.is_file()]
for r in sorted(remaining):
    print(f"  {r}")
