"""Centralized configuration using pydantic-settings.

All configuration flows through this module. Environment variables
are read at instantiation time — restart the server to pick up changes.
"""

from __future__ import annotations

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    """Application settings loaded from environment variables.

    All Router-specific settings use the ``ROUTER_`` prefix convention.
    Provider API keys follow provider naming (``OPENAI_API_KEY``, etc).
    """

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
    )

    # -- Authentication --
    # If set, all API endpoints (except /health) require
    # Authorization: Bearer <token> matching this value.
    # If unset or empty, auth is disabled (local dev mode).
    router_api_key: str | None = None

    # -- CORS --
    # Comma-separated list of allowed origins.
    # Empty string (default): allow localhost on any port.
    # "*": allow all origins (explicit opt-in).
    # Specific: "http://example.com,http://localhost:3000"
    router_cors_origins: str = ""

    # -- Server --
    router_port: int = 3838
    router_log_level: str = "INFO"


def get_settings() -> Settings:
    """Create a Settings instance from current environment.

    Not cached — each call reads current env vars.  This makes
    testing straightforward (set env vars before calling).
    """
    return Settings()
