"""Full pipeline run: load -> generate -> enrich -> publish.

Each phase is its own script and idempotent; this orchestrator just chains
them. Phases that need missing credentials skip themselves with a notice.
"""
import subprocess
import sys
import os

HERE = os.path.dirname(__file__)

PHASES = [
    ("p1_load.py", "load ESCO/O*NET/crosswalk into MSSQL"),
    ("p2_generate.py", "generate core skill packages"),
    ("p2b_add_workflows.py", "inject PR-gate workflows (recruiter)"),
    ("p3a_fetch_jobs.py", "fetch recruiter job ads (JSearch)"),
    ("p3b_store_evidence.py", "store extracted evidence in MSSQL"),
    ("p3c_aggregate.py", "aggregate market percentages into the package"),
    ("p4_publish.py", "publish orgs/repos/marketplace to Gitea"),
    ("p4b_tenant_demo.py", "tenant fork + sanitized community PR"),
]


def main():
    for script, label in PHASES:
        path = os.path.join(HERE, script)
        if not os.path.exists(path):
            print(f"== SKIP {script} ({label}): script not present yet")
            continue
        print(f"\n== {script} — {label}")
        r = subprocess.run([sys.executable, "-u", path])
        if r.returncode != 0:
            sys.exit(f"ABORT: {script} failed with exit code {r.returncode}")
    print("\nPipeline complete.")


if __name__ == "__main__":
    main()
