from __future__ import annotations

import os
from dataclasses import dataclass


def _required_env(name: str) -> str:
    value = os.environ.get(name, "").strip()
    if not value:
        raise RuntimeError(f"Missing required environment variable: {name}")
    return value


@dataclass(frozen=True)
class Settings:
    flask_secret_key: str
    game_api_key: str
    patreon_client_id: str
    patreon_client_secret: str
    patreon_redirect_uri: str
    patreon_campaign_id: str
    database_path: str
    public_base_url: str


def load_settings() -> Settings:
    return Settings(
        flask_secret_key=_required_env("FLASK_SECRET_KEY"),
        game_api_key=_required_env("GAME_API_KEY"),
        patreon_client_id=_required_env("PATREON_CLIENT_ID"),
        patreon_client_secret=_required_env("PATREON_CLIENT_SECRET"),
        patreon_redirect_uri=_required_env("PATREON_REDIRECT_URI"),
        patreon_campaign_id=_required_env("PATREON_CAMPAIGN_ID"),
        database_path=os.environ.get("DATABASE_PATH", "backend/patreon_auth.db"),
        public_base_url=os.environ.get("PUBLIC_BASE_URL", "http://localhost:5000").rstrip("/"),
    )
