"""Runtime compatibility helpers for constrained Kodi Python builds."""

import sys
import types


def ensure_winreg_stub():
    """Provide a `winreg` module when unavailable.

    Some Kodi platforms (notably Xbox) can run Python environments where
    dependencies attempt to import `winreg`, but the stdlib module is absent.
    """
    try:
        import winreg  # noqa: F401
        return
    except Exception:
        pass

    stub = types.ModuleType("winreg")
    stub.HKEY_CLASSES_ROOT = 0
    stub.REG_SZ = 1
    stub.REG_EXPAND_SZ = 2

    class HKEYType(object):
        pass

    def _no_registry(*args, **kwargs):
        raise OSError("Windows registry is not available on this platform")

    stub.HKEYType = HKEYType
    stub.OpenKey = _no_registry
    stub.EnumKey = _no_registry
    stub.QueryValueEx = _no_registry
    stub.CloseKey = lambda *args, **kwargs: None
    sys.modules["winreg"] = stub


ensure_winreg_stub()
