from pathlib import Path

from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict

BASE_DIR = Path(__file__).parent.parent
DB_PATH = BASE_DIR / "db/taskncoffee.db"


class ApiV1Prefix(BaseModel):
    prefix: str = "/v1"
    auth: str = "/auth"
    users: str = "/users"

    @property
    def login_url(self) -> str:
        return f"{self.prefix}{self.auth}"


class ApiPrefix(BaseModel):
    prefix: str = "/api"
    v1: ApiV1Prefix = ApiV1Prefix()

    @property
    def v1_login_url(self) -> str:
        return f"{self.prefix}{self.v1.login_url}"


class DbSettings(BaseModel):
    url: str = f"sqlite+aiosqlite:///{DB_PATH}"
    echo: bool = False


class AccessToken(BaseModel):
    expire_minutes: int
    secret_key: str
    algorithm: str = "HS256"
    token_type: str = "bearer"  # noqa: S105


class RefreshToken(BaseModel):
    expire_days: int


class CorsSettings(BaseModel):
    production: str
    local: list[str] = ["http://localhost:5173", "http://localhost:5174"]


class Settings(BaseSettings):
    api: ApiPrefix = ApiPrefix()
    db: DbSettings = DbSettings()
    access_token: AccessToken
    refresh_token: RefreshToken
    cors_settings: CorsSettings
    model_config = SettingsConfigDict(
        env_file=".env", env_file_encoding="utf-8", env_nested_delimiter="__"
    )


settings = Settings()  # type: ignore
