from pathlib import Path
import importlib.util
import subprocess
import sys
import time

SCRIPT_DIR = Path(__file__).resolve().parent
DOWNLOADER = SCRIPT_DIR / "gfs_downloader.py"
GRID_BUILDER = SCRIPT_DIR / "grid_builder_v2.py"
TILE_GENERATOR = SCRIPT_DIR / "vector_tile_generator.py"

STEP_DEPENDENCIES = {
    "GFS Downloader": ("requests",),
    "Grid Builder v2": ("numpy", "cfgrib"),
    "Vector Tile Generator": ("mercantile", "mapbox_vector_tile"),
}


def check_dependencies():
    missing = []
    for step_name, modules in STEP_DEPENDENCIES.items():
        missing_modules = [module for module in modules if importlib.util.find_spec(module) is None]
        if missing_modules:
            missing.append(f"{step_name}: {', '.join(missing_modules)}")
    return missing


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

    start = time.time()
    command = [sys.executable, "-u", str(script_path)]

    process = subprocess.Popen(
        command,
        cwd=str(SCRIPT_DIR.parent),
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        bufsize=1,
    )

    assert process.stdout is not None
    for line in process.stdout:
        print(line, end="")

    return_code = process.wait()
    if return_code != 0:
        raise RuntimeError(f"{name} failed with exit code {return_code}")

    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"))

    missing_dependencies = check_dependencies()
    if missing_dependencies:
        print("\n" + "!" * 50)
        print("Pipeline Failed: missing runtime dependencies")
        for item in missing_dependencies:
            print("-", item)
        print("!" * 50)
        raise SystemExit(1)

    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 exc:
        print("\n" + "!" * 50)
        print("Pipeline Failed:", str(exc))
        print("!" * 50)
        raise SystemExit(1)


if __name__ == "__main__":
    main()
