# backend/app/config.py
from urllib.parse import quote_plus

from pydantic_settings import BaseSettings
from functools import lru_cache


class Settings(BaseSettings):
    # Database
    DB_HOST: str = "localhost"
    DB_PORT: int = 5432
    DB_NAME: str = "dak_c2s"
    DB_USER: str = "dak_c2s_admin"
    DB_PASSWORD: str = ""

    # JWT
    JWT_SECRET_KEY: str = "change-me-in-production"
    JWT_ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 15
    REFRESH_TOKEN_EXPIRE_DAYS: int = 7

    # SMTP
    SMTP_HOST: str = "smtp.complexcaresolutions.de"
    SMTP_PORT: int = 465
    SMTP_USER: str = "noreply@complexcaresolutions.de"
    SMTP_PASSWORD: str = ""
    SMTP_FROM: str = "noreply@complexcaresolutions.de"

    # App
    APP_NAME: str = "DAK Zweitmeinungs-Portal"
    FRONTEND_BASE_URL: str = "https://dak.complexcaresolutions.de"
    CORS_ORIGINS: str = "http://localhost:5173,https://dak.complexcaresolutions.de"
    MAX_UPLOAD_SIZE: int = 20971520  # 20MB
    VERSICHERUNG_FILTER: str = "DAK"

    @property
    def database_url(self) -> str:
        password = quote_plus(self.DB_PASSWORD)
        return (
            f"postgresql+psycopg2://{self.DB_USER}:{password}"
            f"@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
        )

    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"


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