import os


DEFAULT_CONFIG: dict[str, object] = {
    "SECRET_KEY": None,
    "DATABASE_URL": "sqlite:///l4d2web.db",
    "STATUS_REFRESH_SECONDS": 8,
    "JOB_WORKER_THREADS": 4,
    "JOB_WORKER_ENABLED": True,
    "JOB_WORKER_POLL_SECONDS": 1,
    "STATE_POLLER_INTERVAL_SECONDS": 30,
    "JOB_LOG_REPLAY_LIMIT": 2000,
    "JOB_LOG_LINE_MAX_CHARS": 4096,
    "PORT_RANGE_START": 27015,
    "PORT_RANGE_END": 27115,
    "LIVE_STATE_POLL_SECONDS": 5,
    "LIVE_STATE_QUERY_TIMEOUT_SECONDS": 2.0,
    "LIVE_STATE_STALE_SECONDS": 30,
    "LIVE_STATE_HISTORY_DAYS": 30,
    "LIVE_STATE_RETENTION_EVERY_TICKS": 60,
    "STUCK_SESSION_SECONDS": 60,
    "STEAM_PROFILE_TTL_SECONDS": 86400,
    "STEAM_WEB_API_KEY": "",
}


def _bool_from_env(raw: str) -> bool:
    return raw.lower() not in {"0", "false", "no"}


def load_config() -> dict[str, object]:
    return {
        "SECRET_KEY": os.getenv("SECRET_KEY"),
        "DATABASE_URL": os.getenv("DATABASE_URL", "sqlite:///l4d2web.db"),
        "STATUS_REFRESH_SECONDS": int(os.getenv("STATUS_REFRESH_SECONDS", "8")),
        "JOB_WORKER_THREADS": int(os.getenv("JOB_WORKER_THREADS", "4")),
        "JOB_WORKER_ENABLED": _bool_from_env(os.getenv("JOB_WORKER_ENABLED", "true")),
        "JOB_WORKER_POLL_SECONDS": float(os.getenv("JOB_WORKER_POLL_SECONDS", "1")),
        "STATE_POLLER_INTERVAL_SECONDS": float(os.getenv("STATE_POLLER_INTERVAL_SECONDS", "30")),
        "JOB_LOG_REPLAY_LIMIT": int(os.getenv("JOB_LOG_REPLAY_LIMIT", "2000")),
        "JOB_LOG_LINE_MAX_CHARS": int(os.getenv("JOB_LOG_LINE_MAX_CHARS", "4096")),
        "PORT_RANGE_START": int(os.getenv("LEFT4ME_PORT_RANGE_START", "27015")),
        "PORT_RANGE_END": int(os.getenv("LEFT4ME_PORT_RANGE_END", "27115")),
        "LIVE_STATE_POLL_SECONDS": float(os.getenv("LIVE_STATE_POLL_SECONDS", "5")),
        "LIVE_STATE_QUERY_TIMEOUT_SECONDS": float(os.getenv("LIVE_STATE_QUERY_TIMEOUT_SECONDS", "2.0")),
        "LIVE_STATE_STALE_SECONDS": int(os.getenv("LIVE_STATE_STALE_SECONDS", "30")),
        "LIVE_STATE_HISTORY_DAYS": int(os.getenv("LIVE_STATE_HISTORY_DAYS", "30")),
        "LIVE_STATE_RETENTION_EVERY_TICKS": int(os.getenv("LIVE_STATE_RETENTION_EVERY_TICKS", "60")),
        "STUCK_SESSION_SECONDS": int(os.getenv("STUCK_SESSION_SECONDS", "60")),
        "STEAM_PROFILE_TTL_SECONDS": int(os.getenv("STEAM_PROFILE_TTL_SECONDS", "86400")),
        "STEAM_WEB_API_KEY": os.getenv("STEAM_WEB_API_KEY", ""),
    }
