# OID_analysis/li22b/run_full_pipeline.py
"""Run full Li22b pipeline: A.2 POD -> A.3 LSE -> B (OID) -> C (synthesis).

Usage (after all 50 commands collected):
  PYTHONPATH="src:$PYTHONPATH" conda run -n sr_env python3 src/OID_analysis/li22b/run_full_pipeline.py
"""
import os, sys, subprocess, time

_THIS_DIR = os.path.dirname(os.path.abspath(__file__))
_REPO = os.path.abspath(os.path.join(_THIS_DIR, "..", "..", ".."))
_ENV = os.getenv("CONDA_DEFAULT_ENV", "sr_env")

STEPS = [
    ("Phase A.2: POD on full DB", "phase_a2_pod.py"),
    ("Phase A.3: LSE", "phase_a3_lse.py"),
    ("Phase B: OID + Crossmap + Joint", "phase_b.py"),
    ("Phase C: Synthesis", "phase_c_synthesis.py"),
]


def main():
    for name, script in STEPS:
        fp = os.path.join(_THIS_DIR, script)
        print(f"\n{'='*60}")
        print(f"  {name}")
        print(f"{'='*60}")
        t0 = time.time()
        result = subprocess.run(
            ["conda", "run", "-n", _ENV, "python3", fp],
            cwd=_REPO,
            env={**os.environ, "PYTHONPATH": f"src:{_REPO}/src"},
            capture_output=False,
        )
        elapsed = time.time() - t0
        if result.returncode != 0:
            print(f"  FAILED (exit {result.returncode}) after {elapsed:.0f}s")
            break
        print(f"  Done in {elapsed:.0f}s")
    print("\nPipeline complete.")


if __name__ == "__main__":
    main()
