from functools import lru_cache
from pathlib import Path

from pydantic_settings import BaseSettings, SettingsConfigDict


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

    database_url: str = "postgresql+psycopg://gost:gost@localhost:5432/gost"
    api_token: str = "changeme"
    data_dir: str = "./data"
    font_path: str = str(Path(__file__).resolve().parents[2] / "fonts" / "GOST_A.TTF")

    openrouter_base_url: str = "https://openrouter.ai/api/v1"
    openrouter_api_key: str = ""
    openrouter_model: str = "deepseek/deepseek-chat"
    # SOCKS5 proxy for OpenRouter, e.g. socks5://user:pass@host:1080
    openrouter_proxy: str = ""
    llm_max_context_rows: int = 80

    cors_origins: str = "*"

    @property
    def cors_origin_list(self) -> list[str]:
        if self.cors_origins.strip() == "*":
            return ["*"]
        return [o.strip() for o in self.cors_origins.split(",") if o.strip()]


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