from functools import lru_cache
from pathlib import Path

from pydantic_settings import BaseSettings, SettingsConfigDict

API_DIR = Path(__file__).resolve().parents[1]
REPO_DIR = API_DIR.parent


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=API_DIR / ".env", extra="ignore")

    database_url: str = "postgresql://avyguide:avyguide@127.0.0.1:5436/avyguide"
    # Public origin of the web app, used to build invite links.
    public_base_url: str = "http://localhost:3000"
    # Secure cookies need HTTPS; turn off only for local http development.
    cookie_secure: bool = True
    session_days: int = 14
    guidance_docs_dir: Path = REPO_DIR / "docs" / "guidance"

    # Encrypts secrets saved from the Admin page (app/secrets.py). Keep it only in the env file.
    app_secret_key: str | None = None

    # Claude (optional). A key saved on the Admin page takes precedence; without either the AI endpoints return 503.
    anthropic_api_key: str | None = None
    ai_default_model: str = "claude-opus-5"
    ai_allowed_models: list[str] = ["claude-opus-5", "claude-fable-5-1"]
    ai_monthly_cap_usd: float = 50.0


@lru_cache
def get_settings() -> Settings:
    return Settings()
