"""GPU monitoring state (NVIDIA via pynvml).

Extracted from system routes so core modules can access GPU metrics
without importing from the API layer.
"""

from wled_controller.utils import get_logger

logger = get_logger(__name__)

nvml_available = False
nvml = None
nvml_handle = None

try:
    import pynvml as _pynvml_mod

    _pynvml_mod.nvmlInit()
    nvml_handle = _pynvml_mod.nvmlDeviceGetHandleByIndex(0)
    nvml_available = True
    nvml = _pynvml_mod
    logger.info(f"NVIDIA GPU monitoring enabled: {nvml.nvmlDeviceGetName(nvml_handle)}")
except Exception:
    logger.info("NVIDIA GPU monitoring unavailable (pynvml not installed or no NVIDIA GPU)")
