#!/usr/bin/env python3
"""
专项施工方案数据看板 · 一键管线
用法: python3 run_all.py [YYYY-MM-DD]  # 默认 2026-06-08
流程: B1(方案清洗) → B2(项目跟踪) → B3(营收财务) → B4(看板HTML)
"""
import sys, subprocess, time
from pathlib import Path

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

date = sys.argv[1] if len(sys.argv) > 1 else '2026-06-08'
print(f'🏗️  专项施工方案数据看板 · 管线启动\n   日期: {date}\n')

scripts = [
    ('B1 危大方案清洗', 'b1_methods.py', ['验证方案状态分布、审批率、预警信号']),
    ('B2 项目启动跟踪', 'b2_tracking.py', ['提取中东区域80个项目的启动任务完成情况']),
    ('B3 营收财务统计', 'b3_revenue.py', ['统计47个活跃项目营收数据']),
    ('B4 看板HTML生成', 'b4_dashboard_html.py', ['生成危大方案独立看板+综合看板']),
]

failed = []
start = time.time()

for name, script, checks in scripts:
    path = PROJECT_ROOT / 'src' / script
    if not path.exists():
        print(f'  ⚠️  {name}: 脚本不存在 {path}')
        failed.append(name)
        continue

    print(f'▶ {name} ...')
    try:
        result = subprocess.run(
            ['python3', str(path)],
            capture_output=True, text=True, timeout=120,
            cwd=str(PROJECT_ROOT), env={**__import__('os').environ, 'PYTHONIOENCODING': 'utf-8'}
        )
        if result.returncode != 0:
            print(f'  ❌ 退出码 {result.returncode}')
            print(f'     {result.stderr.strip()[-200:]}')
            failed.append(name)
        else:
            # Show summary lines
            for line in result.stdout.strip().split('\n'):
                if any(kw in line for kw in ['✅', 'Summary', 'Done', '合同', 'Found', 'Top']):
                    print(f'     {line.strip()[:100]}')
            print(f'  ✅ 完成')
    except subprocess.TimeoutExpired:
        print(f'  ❌ 超时')
        failed.append(name)

elapsed = time.time() - start
print(f'\n{"="*50}')
print(f'🏁 管线完成 | 耗时 {elapsed:.0f}s | 成功 {len(scripts)-len(failed)}/{len(scripts)}')
if failed:
    print(f'⚠️ 失败: {", ".join(failed)}')
    sys.exit(1)

# Show output paths
out = PROJECT_ROOT / 'data' / date / 'cleaned'
print(f'\n📁 输出目录: {out}')
for f in sorted(out.glob('*.html')):
    print(f'   🌐 {f.name}')
for f in sorted(out.glob('*.parquet')):
    print(f'   📊 {f.name}')
for f in sorted(out.glob('*_validation.json')):
    print(f'   📋 {f.name}')
