"""Centralized configuration via Pydantic Settings.

All environment variables are declared here with types, defaults, and
validation.  Reads ``.env`` automatically via ``pydantic-settings``.

Usage::

    from core.config import get_settings

    settings = get_settings()
    key = settings.openrouter_api_key
"""

from __future__ import annotations

from functools import lru_cache
from typing import Literal

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    """Global runtime settings — single source of truth for env vars."""

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

    # -- LLM Provider Keys ------------------------------------------------
    openrouter_api_key: str | None = None
    openai_api_key: str | None = None
    anthropic_api_key: str | None = None
    google_api_key: str | None = None
    ollama_host: str = "http://localhost:11434"

    # -- OpenRouter Adapter -----------------------------------------------
    openrouter_timeout_s: float = 120.0
    openrouter_referer: str = "https://albusos.dev"
    openrouter_title: str = "AlbusOS"

    # -- Model Routing Overrides ------------------------------------------
    # Set ALBUS_MODEL_FAST, ALBUS_MODEL_REASONING, etc. to override defaults.
    albus_model_fast: str | None = None
    albus_model_reasoning: str | None = None
    albus_model_code: str | None = None
    albus_model_vision: str | None = None
    albus_model_embedding: str | None = None
    albus_model_local: str | None = None

    # -- Code Sandbox -----------------------------------------------------
    agent_stdlib_code_sandbox_mode: Literal["local", "docker"] = "local"
    albus_code_sandbox_docker_image_datascience: str = "albus-sandbox-datascience:latest"
    albus_code_sandbox_docker_image_viz: str = "albus-sandbox-viz:latest"
    albus_code_sandbox_docker_image_web: str = "albus-sandbox-web:latest"
    agent_stdlib_code_sandbox_docker_image: str = "python:3.11-slim"

    # -- Workspace / Paths ------------------------------------------------
    albus_workspace: str | None = None
    albus_root: str | None = None
    albus_store_path: str = ".albus_store"

    # -- Observability ----------------------------------------------------
    albus_log_level: str | None = None
    albus_log_format: str = ""
    albus_no_color: str = ""
    albus_http_trace: str = ""
    albus_access_log: str = ""

    # -- LLM Retry --------------------------------------------------------
    albus_llm_max_retries: int = Field(default=3, ge=0, le=10)
    albus_llm_retry_base_delay: float = Field(default=1.0, ge=0.1, le=30.0)

    # -- Helpers ----------------------------------------------------------

    def get_model_override(self, capability: str) -> str | None:
        """Return the env-var model override for a capability, or None."""
        attr = f"albus_model_{capability.lower()}"
        return getattr(self, attr, None)

    @property
    def workspace_dir(self) -> str:
        """Effective workspace directory."""
        import os

        return self.albus_workspace or os.getcwd()


@lru_cache(maxsize=1)
def get_settings() -> Settings:
    """Return the cached global settings instance."""
    return Settings()


__all__ = [
    "Settings",
    "get_settings",
]
