#!/usr/bin/env python3
"""Environment preflight check: python version, CANN toolkits, npu-smi.
Exit 0 if OK, 1 if missing critical deps (safe to run without NPU)."""
import shutil, subprocess, sys


def check(cmd):
    p = shutil.which(cmd.split()[0])
    if not p:
        return False, f"{cmd.split()[0]} not found"
    r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=15)
    return r.returncode == 0, (r.stdout or r.stderr).strip().splitlines()[0] if (r.stdout or r.stderr).strip() else "ok"


def main():
    checks = [
        ("python", check("python3 --version")),
        ("npu-smi", check("npu-smi info" )),
    ]
    critical = ["python"]
    for tool, (ok, detail) in checks:
        tag = "OK " if ok else "MISS"
        print(f"[{tag}] {tool}: {detail}")
        if not ok and tool in critical:
            print("CRITICAL dependency missing; cannot run skill flows")
            return 1
    return 0


if __name__ == "__main__":
    sys.exit(main())