"""Global configuration via environment variables."""

from __future__ import annotations

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    """Global application settings loaded from environment."""

    openai_api_key: str = ""
    database_url: str = ""  # Empty = SQLite, postgres://... = PostgreSQL
    # PostgreSQL connection pool size. Raised from a hardcoded 5 after a
    # crystallise-master prod incident where a normal page-load burst
    # exhausted the pool within 14s of cold-start. The postgres container's
    # max_connections defaults to 100, so leave plenty of headroom.
    db_pool_size: int = 20

    # Defaults
    default_model: str = "gpt-5-nano"
    clustering_model: str = "gpt-4.1"
    max_concurrent_requests: int = 10

    # Screening defaults
    default_repetitions: int = 5
    default_threshold: float = 1.0
    max_clusters: int = 20

    # API server
    api_host: str = "0.0.0.0"
    api_port: int = 8005
    cors_origins: list[str] = ["*"]

    # Auth
    api_keys: str = ""  # Comma-separated list of valid API keys (empty = accept any key in dev mode)

    model_config = {"env_prefix": "CRYSTALLISE_", "env_file": ".env", "extra": "ignore"}


def get_settings() -> Settings:
    """Get application settings (cached)."""
    return Settings()
