from typing import Any, Self

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class MongoDBSettings(BaseSettings):
    model_config = SettingsConfigDict(env_prefix="SNAKESTACK_MONGODB_")

    uri: str = Field(
        default="mongodb://localhost:27017",
        description=""
    )
    dbname: str = Field(
        default="snakestack",
        description=""
    )
    user: str | None = Field(
        default=None,
        description=""
    )
    password: str | None = Field(
        default=None,
        description=""
    )
    db_auth_source: str = Field(
        default="admin",
        description=""
    )

    def get_host(self: Self) -> str:
        return f"{self.uri}/{self.dbname}"

    def get_kwargs(self: Self) -> dict[str, Any]:
        config = {}
        if self.user and self.password:
            config["username"] = self.user
            config["password"] = self.password
            config["authSource"] = self.db_auth_source

        return config
