"""重跑 DWS_ASSISTANT_DAILY (全量) + DWS_ASSISTANT_MONTHLY 验证 SCD2 修复"""
import subprocess
import sys

# 第一步：全量重跑 daily（覆盖历史数据，修复 NULL level_name）
steps = [
    {
        "task": "DWS_ASSISTANT_DAILY",
        "extra_args": ["--force-full", "--window-start", "2025-01-01", "--window-end", "2026-02-27", "--force-window-override"],
    },
    {
        "task": "DWS_ASSISTANT_MONTHLY",
        "extra_args": ["--force-full", "--window-start", "2025-01-01", "--window-end", "2026-02-27", "--force-window-override"],
    },
]

for step in steps:
    task = step["task"]
    print(f"\n{'='*60}")
    print(f"重跑 {task}")
    print(f"{'='*60}")
    cmd = [
        sys.executable, "-m", "cli.main",
        "--tasks", task,
    ] + step.get("extra_args", [])
    result = subprocess.run(
        cmd,
        cwd="apps/etl/connectors/feiqiu",
        capture_output=True,
        text=True,
    )
    print(result.stdout[-3000:] if len(result.stdout) > 3000 else result.stdout)
    if result.stderr:
        print("STDERR:", result.stderr[-2000:] if len(result.stderr) > 2000 else result.stderr)
    if result.returncode != 0:
        print(f"❌ {task} 失败 (exit={result.returncode})")
        break
    else:
        print(f"✅ {task} 完成")
