# -*- coding: utf-8 -*-
"""批量替换旧 schema 引用为新名称。

替换规则：
  billiards_ods  → ods
  billiards_dwd  → dwd
  billiards_dws  → dws
  etl_admin      → meta  （仅在 SQL schema 上下文中）

注意：etl_admin 替换需要更精细的控制，避免误改文件名引用。
"""
import pathlib
import sys

ROOT = pathlib.Path(__file__).resolve().parents[1]

# ── 简单全文替换（billiards_xxx → xxx）──────────────────────
SIMPLE_REPLACEMENTS = {
    "billiards_ods.": "ods.",
    "billiards_dwd.": "dwd.",
    "billiards_dws.": "dws.",
    "'billiards_ods'": "'ods'",
    "'billiards_dwd'": "'dwd'",
    "'billiards_dws'": "'dws'",
    '"billiards_ods"': '"ods"',
    '"billiards_dwd"': '"dwd"',
    '"billiards_dws"': '"dws"',
}

# ── etl_admin SQL schema 引用替换 ──────────────────────────
ETL_ADMIN_SQL_REPLACEMENTS = {
    "etl_admin.etl_task": "meta.etl_task",
    "etl_admin.etl_cursor": "meta.etl_cursor",
    "etl_admin.etl_run": "meta.etl_run",
    "etl_admin.run_tracker": "meta.etl_run",
    "etl_admin.run_status_enum": "meta.run_status_enum",
    "'etl_admin'": "'meta'",
    '"etl_admin"': '"meta"',
}

# ── 需要处理的文件列表 ─────────────────────────────────────
FILES_SIMPLE = [
    # ETL 非测试代码
    "apps/etl/connectors/feiqiu/tasks/verification/ods_verifier.py",
    "apps/etl/connectors/feiqiu/tasks/verification/index_verifier.py",
    "apps/etl/connectors/feiqiu/tasks/utility/manual_ingest_task.py",
    "apps/etl/connectors/feiqiu/tasks/utility/seed_dws_config_task.py",
    # ETL 脚本
    "apps/etl/connectors/feiqiu/scripts/rebuild/rebuild_db_and_run_ods_to_dwd.py",
    # ETL 集成测试
    "apps/etl/connectors/feiqiu/tests/integration/test_index_tasks.py",
    # GUI
    "gui/workers/db_worker.py",
    "gui/workers/task_worker.py",
    "gui/widgets/status_panel.py",
    "gui/widgets/db_viewer.py",
    "gui/models/task_registry.py",
    # 配置
    ".env.template",
]

FILES_ETL_ADMIN = [
    "apps/etl/connectors/feiqiu/orchestration/cursor_manager.py",
    "apps/etl/connectors/feiqiu/orchestration/run_tracker.py",
    "apps/etl/connectors/feiqiu/orchestration/task_executor.py",
    "apps/etl/connectors/feiqiu/tasks/utility/check_cutoff_task.py",
    "apps/etl/connectors/feiqiu/scripts/rebuild/rebuild_db_and_run_ods_to_dwd.py",
]


def replace_in_file(filepath: pathlib.Path, replacements: dict) -> int:
    """在文件中执行替换，返回替换次数。"""
    if not filepath.exists():
        return -1
    text = filepath.read_text(encoding="utf-8")
    new_text = text
    total = 0
    for old, new in replacements.items():
        count = new_text.count(old)
        total += count
        new_text = new_text.replace(old, new)
    if total > 0:
        filepath.write_text(new_text, encoding="utf-8")
    return total


def main():
    print("=== 批量 schema 重命名 ===\n")

    # 1) 简单替换
    print("── billiards_xxx → xxx ──")
    for rel in FILES_SIMPLE:
        p = ROOT / rel
        n = replace_in_file(p, SIMPLE_REPLACEMENTS)
        if n == -1:
            print(f"  SKIP (不存在): {rel}")
        elif n == 0:
            print(f"  SKIP (无匹配): {rel}")
        else:
            print(f"  OK: {rel} ({n} 处)")

    # 2) etl_admin SQL 替换
    print("\n── etl_admin → meta ──")
    for rel in FILES_ETL_ADMIN:
        p = ROOT / rel
        n = replace_in_file(p, ETL_ADMIN_SQL_REPLACEMENTS)
        if n == -1:
            print(f"  SKIP (不存在): {rel}")
        elif n == 0:
            print(f"  SKIP (无匹配): {rel}")
        else:
            print(f"  OK: {rel} ({n} 处)")

    print("\n完成。")


if __name__ == "__main__":
    main()
