"""Application settings via pydantic-settings.

Nested fields use the ``__`` separator (pydantic-settings convention):
``DATABASE__URL`` -> ``settings.database.url``.
"""

from __future__ import annotations

from enum import StrEnum

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class Environment(StrEnum):
    DEV = "dev"
    STAGING = "staging"
    PROD = "prod"


class ServerSettings(BaseSettings):
    host: str = "0.0.0.0"
    port: int = 8000


{% if cookiecutter.use_db == "yes" %}
class DatabaseSettings(BaseSettings):
    url: str = "sqlite://db.sqlite3"
{% endif %}


{% if cookiecutter.use_auth == "yes" %}
class JWTSettings(BaseSettings):
    secret: str = "change-me-in-production"
    algorithm: str = "HS256"
    expire_minutes: int = 60
{% endif %}


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        env_nested_delimiter="__",
        extra="ignore",
    )

    environment: Environment = Environment.DEV
    server: ServerSettings = Field(default_factory=ServerSettings)
{% if cookiecutter.use_db == "yes" %}
    database: DatabaseSettings = Field(default_factory=DatabaseSettings)
{% endif %}
{% if cookiecutter.use_auth == "yes" %}
    jwt: JWTSettings = Field(default_factory=JWTSettings)
{% endif %}
    ip_whitelist: list[str] = Field(default_factory=lambda: ["127.0.0.1", "::1"])


settings = Settings()