from functools import lru_cache
from typing import Optional

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    project_name: str = "Rail Game API"
    version: str = "0.1.0"
    api_prefix: str = "/api"
    jwt_secret_key: str = "insecure-change-me"
    jwt_algorithm: str = "HS256"
    access_token_expire_minutes: int = 60
    database_url: str = "postgresql+psycopg://railgame:railgame@localhost:5432/railgame"
    database_echo: bool = False
    test_database_url: Optional[str] = None
    alembic_database_url: Optional[str] = None

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

    @property
    def sqlalchemy_database_url(self) -> str:
        return self.database_url

    @property
    def sqlalchemy_test_url(self) -> Optional[str]:
        return self.test_database_url

    @property
    def sqlalchemy_alembic_url(self) -> str:
        return self.alembic_database_url or self.database_url


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