import os
import subprocess
import time

DOWNLOADER = "gfs_downloader.py"
GRID_BUILDER = "grid/grid_builder_v2.py"
TILE_GENERATOR = "tiles/vector_tile_generator.py"


def run_step(name, script):
    print("\n==========================")
    print("Running:", name)
    print("==========================\n")

    start = time.time()

    result = subprocess.run(
        ["python", script],
        capture_output=True,
        text=True
    )

    print(result.stdout)

    if result.returncode != 0:
        print("ERROR:", result.stderr)
        raise RuntimeError(name + " failed")

    end = time.time()

    print("\nFinished:", name)
    print("Time:", round(end-start,2),"seconds")


def main():
    print("Weather Pipeline Starting...")
    print("Date:", time.strftime("%Y-%m-%d %H:%M:%S"))

    try:
        run_step("GFS Downloader", DOWNLOADER)
        run_step("Grid Builder v2", GRID_BUILDER)
        run_step("Vector Tile Generator", TILE_GENERATOR)

        print("\n" + "="*50)
        print("Weather Pipeline Completed Successfully!")
        print("="*50)

    except RuntimeError as e:
        print("\n" + "!"*50)
        print("Pipeline Failed:", str(e))
        print("!"*50)
        exit(1)


if __name__ == "__main__":
    main()
