import os
from dataclasses import dataclass

from dotenv import load_dotenv

load_dotenv()


@dataclass(frozen=True)
class Config:
    bot_token: str
    bot_username: str
    log_level: str
    drop_pending_updates: bool
    data_dir: str
    poll_interval: int
    steam_api_key: str
    stratz_api_key: str

    @classmethod
    def from_env(cls) -> "Config":
        bot_token = os.environ.get("BOT_TOKEN", "").strip()
        if not bot_token:
            raise RuntimeError(
                "BOT_TOKEN environment variable is not set. "
                "Set it in .env or pass it to the container."
            )

        bot_username = os.environ.get("BOT_USERNAME", "").strip().lstrip("@")
        if not bot_username:
            raise RuntimeError(
                "BOT_USERNAME environment variable is not set. "
                "Set it to your bot's username without the @ symbol."
            )

        return cls(
            bot_token=bot_token,
            bot_username=bot_username,
            log_level=os.environ.get("LOG_LEVEL", "INFO"),
            drop_pending_updates=os.environ.get(
                "DROP_PENDING_UPDATES", "true"
            ).lower() in ("1", "true", "yes"),
            data_dir=os.environ.get("DATA_DIR", "/data").rstrip("/"),
            poll_interval=int(os.environ.get("POLL_INTERVAL_SECONDS", "300")),
            steam_api_key=os.environ.get("STEAM_API_KEY", ""),
            stratz_api_key=os.environ.get("STRATZ_API_KEY", ""),
        )


config = Config.from_env()
