#!/usr/bin/env python3
"""
detect-torch.py — Reusable PyTorch version and install URL detection.

Detects the current platform/architecture/GPU environment and outputs
the correct pip install arguments for PyTorch + torchaudio.

Supports:
  - x86_64 Linux with NVIDIA CUDA (cu118, cu121, cu124)
  - x86_64 Linux CPU-only
  - aarch64 Linux (ARM) CPU-only
  - aarch64 Linux Jetson (NVIDIA GPU via JetPack wheels)
  - macOS x86_64 (CPU)
  - macOS arm64 / Apple Silicon (MPS via default PyPI)
  - Windows x86_64 (CUDA or CPU)

Output: JSON to stdout with fields:
  {
    "platform": "linux",
    "arch": "x86_64" | "aarch64" | "arm64",
    "accelerator": "cuda" | "mps" | "cpu",
    "cuda_version": "12.4" | null,
    "is_jetson": false,
    "pip_args": ["torch", "torchaudio", "--index-url", "..."],
    "pip_args_str": "torch torchaudio --index-url ...",
    "description": "x86_64 Linux CUDA 12.4"
  }

Usage from shell:
  python3 scripts/detect-torch.py
  python3 scripts/detect-torch.py --pip-args   # just the pip install args
  python3 scripts/detect-torch.py --index-url  # just the --index-url value

Usage from Node.js (via child_process):
  const result = execSync('python3 scripts/detect-torch.py').toString();
  const spec = JSON.parse(result);
  await shell(`pip install ${spec.pip_args_str}`);

Derived from hydra/service_router/.services/depth_any/app.py bootstrap logic.
"""

import json
import os
import platform
import subprocess
import sys
from pathlib import Path


def detect_cuda_version():
    """Detect NVIDIA CUDA version from nvidia-smi or nvcc."""
    # Try nvidia-smi first (works even without nvcc)
    try:
        result = subprocess.run(
            ["nvidia-smi", "--query-gpu=driver_version", "--format=csv,noheader,nounits"],
            capture_output=True, text=True, timeout=5
        )
        if result.returncode == 0:
            # nvidia-smi works — now get CUDA version
            result2 = subprocess.run(
                ["nvidia-smi"],
                capture_output=True, text=True, timeout=5
            )
            if result2.returncode == 0:
                import re
                match = re.search(r"CUDA Version:\s*(\d+\.\d+)", result2.stdout)
                if match:
                    return match.group(1)
    except Exception:
        pass

    # Try nvcc (Jetson, or systems with CUDA toolkit)
    try:
        result = subprocess.run(
            ["nvcc", "--version"],
            capture_output=True, text=True, timeout=5
        )
        if result.returncode == 0:
            import re
            match = re.search(r"release (\d+\.\d+)", result.stdout)
            if match:
                return match.group(1)
    except Exception:
        pass

    return None


def is_jetson():
    """Detect NVIDIA Jetson (aarch64 + /etc/nv_tegra_release)."""
    return (
        platform.machine().lower() in ("aarch64", "arm64")
        and Path("/etc/nv_tegra_release").exists()
    )


def jetson_torch_wheel_url():
    """Find the best Jetson PyTorch wheel URL from NVIDIA's index."""
    import re
    import urllib.request

    py_tag = f"cp{sys.version_info.major}{sys.version_info.minor}"

    # Check env override first
    env_wheel = os.environ.get("JETSON_TORCH_WHEEL")
    if env_wheel:
        return env_wheel

    # Try NVIDIA's Jetson wheel index
    release_text = ""
    try:
        release_text = Path("/etc/nv_tegra_release").read_text()
    except Exception:
        pass

    # Determine JetPack version -> index URL
    indexes = []
    if "R36" in release_text and "REVISION: 4" in release_text:
        indexes.extend([
            "https://developer.download.nvidia.com/compute/redist/jp/v61",
            "https://developer.download.nvidia.com/compute/redist/jp/v60",
        ])
    elif "R36" in release_text:
        indexes.append("https://developer.download.nvidia.com/compute/redist/jp/v60")
    elif "R35" in release_text:
        indexes.append("https://developer.download.nvidia.com/compute/redist/jp/v51")
    else:
        indexes.extend([
            "https://developer.download.nvidia.com/compute/redist/jp/v60",
            "https://developer.download.nvidia.com/compute/redist/jp/v51",
        ])

    for idx_url in indexes:
        try:
            with urllib.request.urlopen(f"{idx_url}/pytorch/", timeout=10) as resp:
                html = resp.read().decode("utf-8", "ignore")
            pattern = rf'href="(torch-([0-9a-zA-Z\\.\\+]+)\\.nv[^"]*{py_tag}[^"]*linux_aarch64\\.whl)"'
            matches = re.findall(pattern, html)
            best = None
            for fname, ver in matches:
                base_ver = ver.split("+")[0]
                parts = tuple(int(x) for x in base_ver.split(".")[:3] if x.isdigit())
                if best is None or parts > best[0]:
                    best = (parts, fname, base_ver)
            if best:
                return f"{idx_url}/pytorch/{best[1]}"
        except Exception:
            continue

    return None


def detect_torch_spec():
    """Detect the optimal PyTorch install spec for this system."""
    machine = platform.machine().lower()
    system = platform.system().lower()

    spec = {
        "platform": system,
        "arch": machine,
        "accelerator": "cpu",
        "cuda_version": None,
        "is_jetson": False,
        "pip_args": [],
        "pip_args_str": "",
        "torchaudio_version": None,
        "error": None,
        "description": "",
    }

    # ── macOS ──────────────────────────────────────────────────────────
    if system == "darwin":
        if machine in ("arm64", "aarch64"):
            # Apple Silicon — MPS acceleration via default PyPI wheels
            spec["accelerator"] = "mps"
            spec["pip_args"] = ["torch", "torchaudio"]
            spec["description"] = "macOS Apple Silicon (MPS)"
        else:
            # Intel Mac — CPU only
            spec["pip_args"] = ["torch", "torchaudio"]
            spec["description"] = "macOS Intel (CPU)"
        spec["pip_args_str"] = " ".join(spec["pip_args"])
        return spec

    # ── Windows ────────────────────────────────────────────────────────
    if system == "windows":
        cuda_ver = detect_cuda_version()
        if cuda_ver:
            spec["accelerator"] = "cuda"
            spec["cuda_version"] = cuda_ver
            major_minor = cuda_ver.split(".")
            cu_tag = f"cu{major_minor[0]}{major_minor[1]}" if len(major_minor) >= 2 else "cu124"
            spec["pip_args"] = ["torch", "torchaudio", "--index-url", f"https://download.pytorch.org/whl/{cu_tag}"]
            spec["description"] = f"Windows CUDA {cuda_ver}"
        else:
            spec["pip_args"] = ["torch", "torchaudio", "--index-url", "https://download.pytorch.org/whl/cpu"]
            spec["description"] = "Windows CPU"
        spec["pip_args_str"] = " ".join(spec["pip_args"])
        return spec

    # ── Linux ──────────────────────────────────────────────────────────

    # Check for Jetson first (aarch64 + NVIDIA GPU via JetPack)
    if is_jetson():
        spec["is_jetson"] = True
        spec["accelerator"] = "cuda"
        spec["cuda_version"] = detect_cuda_version()

        wheel_url = jetson_torch_wheel_url()
        if wheel_url:
            # Jetson needs wheel URL directly (not index-url)
            spec["pip_args"] = [wheel_url]
            spec["pip_args_str"] = wheel_url
            import re
            version_match = re.search(r"/torch-(\d+)\.(\d+)", wheel_url)
            if version_match:
                spec["torchaudio_version"] = f"{version_match.group(1)}.{version_match.group(2)}.0"
            spec["description"] = f"Jetson aarch64 CUDA {spec['cuda_version'] or '?'} (JetPack wheel)"
        else:
            # Generic PyPI ARM Torch is not a JetPack ABI contract. Fail
            # closed instead of installing a wheel that can report an old or
            # insufficient driver on otherwise healthy Orin hardware.
            spec["error"] = (
                "No environment-matched NVIDIA Jetson Torch wheel was found. "
                "Set JETSON_TORCH_WHEEL to an exact wheel for this L4T, CUDA, "
                "Python ABI, and architecture."
            )
            spec["description"] = f"Jetson aarch64 CUDA {spec['cuda_version'] or '?'} (unsupported until an exact wheel is supplied)"
        return spec

    # Non-Jetson aarch64 (ARM server, Raspberry Pi, etc.)
    if machine in ("aarch64", "arm64", "armv7l"):
        cuda_ver = detect_cuda_version()
        if cuda_ver:
            spec["accelerator"] = "cuda"
            spec["cuda_version"] = cuda_ver
            # ARM + CUDA (e.g., Grace Hopper) — use generic pip (PyTorch publishes aarch64+cuda wheels on PyPI)
            spec["pip_args"] = ["torch", "torchaudio"]
            spec["description"] = f"Linux aarch64 CUDA {cuda_ver}"
        else:
            # ARM CPU — PyTorch publishes aarch64 CPU wheels on PyPI since 2.0+
            # Do NOT use --index-url whl/cpu (no aarch64 there)
            spec["pip_args"] = ["torch", "torchaudio"]
            spec["description"] = "Linux aarch64 CPU"
        spec["pip_args_str"] = " ".join(spec["pip_args"])
        return spec

    # x86_64 Linux
    cuda_ver = detect_cuda_version()
    if cuda_ver:
        spec["accelerator"] = "cuda"
        spec["cuda_version"] = cuda_ver
        # Map CUDA version to PyTorch index tag
        major = int(cuda_ver.split(".")[0])
        minor = int(cuda_ver.split(".")[1]) if "." in cuda_ver else 0
        if major >= 12 and minor >= 4:
            cu_tag = "cu124"
        elif major >= 12 and minor >= 1:
            cu_tag = "cu121"
        elif major >= 11 and minor >= 8:
            cu_tag = "cu118"
        else:
            cu_tag = "cu121"  # default fallback
        spec["pip_args"] = ["torch", "torchaudio", "--index-url", f"https://download.pytorch.org/whl/{cu_tag}"]
        spec["description"] = f"Linux x86_64 CUDA {cuda_ver} ({cu_tag})"
    else:
        spec["pip_args"] = ["torch", "torchaudio", "--index-url", "https://download.pytorch.org/whl/cpu"]
        spec["description"] = "Linux x86_64 CPU"

    spec["pip_args_str"] = " ".join(spec["pip_args"])
    return spec


def main():
    spec = detect_torch_spec()

    if len(sys.argv) > 1:
        flag = sys.argv[1]
        if flag == "--pip-args":
            print(spec["pip_args_str"])
            return
        elif flag == "--index-url":
            for i, arg in enumerate(spec["pip_args"]):
                if arg == "--index-url" and i + 1 < len(spec["pip_args"]):
                    print(spec["pip_args"][i + 1])
                    return
            print("")  # no index-url (use default PyPI)
            return
        elif flag == "--description":
            print(spec["description"])
            return
        elif flag == "--accelerator":
            print(spec["accelerator"])
            return
        elif flag == "--json":
            pass  # fall through to JSON output
        else:
            print(f"Unknown flag: {flag}", file=sys.stderr)
            print("Usage: detect-torch.py [--pip-args|--index-url|--description|--accelerator|--json]", file=sys.stderr)
            sys.exit(1)

    print(json.dumps(spec, indent=2))


if __name__ == "__main__":
    main()
