import subprocess
import sys
import os

def pause():
    print("\n========================================")
    print("   Step complete. Press ENTER to continue")
    print("========================================")
    input()
    print()

def run_step(description, command):
    print(f"\n=== {description} ===")
    print("Running:", " ".join(command))
    result = subprocess.run(command)
    if result.returncode != 0:
        print(f"ERROR: {description} failed.")
        sys.exit(result.returncode)

    pause()

def main():
    if len(sys.argv) != 2:
        print("Usage: python step0run_pipeline.py baseName")
        print("Example: python step0run_pipeline.py ballLocation02")
        sys.exit(1)

    # Normalize base name (handles base or base.json)
    base = os.path.splitext(sys.argv[1])[0]

    print("\n========================================")
    print("      Bocce Measurement Pipeline")
    print("========================================")
    print(f"Starting pipeline for base name: {base}\n")

    # ----------------------------------------
    # Derive all filenames
    # ----------------------------------------
    json_file = f"{base}.json"
    png_step1 = f"{base}.png"
    png_step2 = f"{base}_perspective.png"
    png_step3 = f"{base}_ortho.png"
    measured_json = f"{base}_measured.json"

    # ----------------------------------------
    # Step 1
    # ----------------------------------------
    pause()
    run_step(
        "Step 1: Render orthophoto from JSON",
        ["python", "step1photoFromJson.py", json_file]
    )

    # ----------------------------------------
    # Step 2
    # ----------------------------------------
    run_step(
        "Step 2: Generate perspective image",
        ["python", "step2perspective_from_ortho.py", png_step1]
    )

    # ----------------------------------------
    # Step 3
    # ----------------------------------------
    run_step(
        "Step 3: Recover ortho image",
        ["python", "step3ortho_from_perspective.py", png_step2]
    )

    # ----------------------------------------
    # Step 4
    # ----------------------------------------
    run_step(
        "Step 4: Analyze ortho image",
        ["python", "step4analyzeOrtho.py", png_step3]
    )

    # ----------------------------------------
    # Step 5
    # ----------------------------------------
    run_step(
        "Step 5: Report results",
        ["python", "step5reportResults.py", json_file]
    )

    # ----------------------------------------
    # Summary
    # ----------------------------------------
    print("\n========================================")
    print("           Pipeline Complete")
    print("========================================")
    print(f"Input JSON:           {json_file}")
    print(f"Step 1 output:        {png_step1}")
    print(f"Step 2 output:        {png_step2}")
    print(f"Step 3 output:        {png_step3}")
    print(f"Measured JSON:        {measured_json}")
    print("\nAll steps executed successfully.\n")

if __name__ == "__main__":
    main()