from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
MANIFEST = json.loads((HERE / "manifest.json").read_text())


def main(argv: list[str]) -> int:
    if "--list" in argv:
        for item in MANIFEST["public"]:
            print(f"{item['id']}: {item['title']}")
        return 0

    selected: set[str] = set()
    for index, arg in enumerate(argv):
        if arg == "--scenario" and index + 1 < len(argv):
            selected.add(argv[index + 1])

    scenarios = MANIFEST["public"]
    if selected:
        scenarios = [item for item in scenarios if item["id"] in selected]

    repo_root = HERE.parents[4]
    manifest_path = repo_root / "sdk" / "rust" / "mubit-sdk" / "Cargo.toml"
    for item in scenarios:
        print(f"==> {item['target']}")
        subprocess.run([
            "cargo", "run", "--manifest-path", str(manifest_path), "--example", item["target"],
        ], check=True)
        print()
    return 0


if __name__ == "__main__":
    raise SystemExit(main(sys.argv[1:]))
