"""
ingest_all_maintenance.py
─────────────────────────
Runs all the maintenance-related ingestion scripts in the correct order.
Run from backend/ directory:
    python scripts/ingest_all_maintenance.py
"""

import subprocess
import sys
import os

def run_script(script_name):
    script_path = os.path.join(os.path.dirname(__file__), script_name)
    print(f"\n=======================================================")
    print(f"▶ RUNNING: {script_name}")
    print(f"=======================================================\n")
    
    result = subprocess.run([sys.executable, script_path])
    
    if result.returncode != 0:
        print(f"\n❌ ERROR: {script_name} failed with exit code {result.returncode}")
        sys.exit(result.returncode)
    else:
        print(f"\n✅ SUCCESS: {script_name} completed successfully.\n")

if __name__ == "__main__":
    scripts_to_run = [
        "seed_maintenance.py",
        "ingest_history_cards.py",
        "ingest_incoming_materials_v2.py",
        "ingest_monthly_stock.py",
        "ingest_machine_cost.py",
        "ingest_pm_schedule.py"
    ]
    
    print("Starting full maintenance data ingestion...")
    
    for script in scripts_to_run:
        run_script(script)
        
    print("🎉 All maintenance data ingested successfully!")
