import os
from functools import lru_cache
from dataclasses import dataclass


@dataclass(frozen=True)
class WebSearchSettings:
    public_base_url: str
    default_max_results: int
    default_region: str
    request_timeout: int


def _int_env(name: str, default: int) -> int:
    raw = os.environ.get(name)
    if raw is None or raw == "":
        return default
    try:
        return int(raw)
    except ValueError:
        return default


@lru_cache
def get_web_search_settings() -> WebSearchSettings:
    base_url = os.environ.get("BASE_URL", "https://taller-wox.fitlabs.dev").rstrip("/")
    public_base_url = os.environ.get(
        "WEB_SEARCH_PUBLIC_BASE_URL",
        f"{base_url}/web-search",
    ).rstrip("/")
    return WebSearchSettings(
        public_base_url=public_base_url,
        default_max_results=_int_env("WEB_SEARCH_DEFAULT_MAX_RESULTS", 5),
        default_region=os.environ.get("WEB_SEARCH_DEFAULT_REGION", "wt-wt"),
        request_timeout=_int_env("WEB_SEARCH_REQUEST_TIMEOUT", 12),
    )
