"""Runtime detection, logging setup and platform helpers."""

from __future__ import annotations

import logging
import os
import platform
import sys


log = logging.getLogger(__name__)


def is_r36s() -> bool:
    """Heuristic: running on aarch64/armv7l with a small screen."""
    return platform.machine() in ("aarch64", "armv7l")


def configure_logging() -> None:
    level_name = os.environ.get("R36S_LOG_LEVEL", "INFO").upper()
    level = getattr(logging, level_name, logging.INFO)
    fmt = "%(asctime)s %(name)s %(levelname)s %(message)s"
    logging.basicConfig(level=level, format=fmt, stream=sys.stderr)


def sdl_env_setup() -> None:
    """Set SDL environment hints for the R36S if needed."""
    if is_r36s():
        # Force fbdev or kmsdrm if no display server
        if not os.environ.get("DISPLAY") and not os.environ.get("WAYLAND_DISPLAY"):
            os.environ.setdefault("SDL_VIDEODRIVER", "kmsdrm")
        os.environ.setdefault("SDL_AUDIODRIVER", "alsa")
