from enum import StrEnum

from pydantic_settings import BaseSettings, SettingsConfigDict


class EnvEnum(StrEnum):
    development = "development"
    production = "production"


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        env_file=".env",
        extra="ignore",
    )

    environment: EnvEnum = EnvEnum.development

    echo_sql: bool = False
    secret_key: str
    cookie_expire_minutes: int = 60
    cookie_domain: str | None = None
    cors_origins: list[str] = ["http://localhost:8000"]
    frontend_url: str = "http://localhost:8000"
    frontend_oauth_error_url: str = "http://localhost:8000/oauth-error"

    google_client_id: str = "test-client-id"
    google_client_secret: str = "test-client-secret"
    google_redirect_uri: str = "http://localhost:8000/v1/auth/google/callback"
    google_admin_email: str | None = None
    google_group_email: str | None = None
    google_service_account_file: str | None = None
    
    api_base_url: str = "https://api.campflow.de"
    api_auth_token: str = ""
    encryption_key: str = ""  # Fernet base64 key — generate with: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
    super_admin_email: str | None = None  # auto-promoted to is_admin=True on startup

    smtp_server: str = "smtp.gmail.com"
    smtp_port: int = 465
    email_account: str | None = None
    email_password: str | None = None
    auto_mail_recipient: str | None = None

    postgres_user: str = "local"
    postgres_password: str
    postgres_host: str = "localhost"
    postgres_port: int = 5432
    postgres_db: str = "local"

    @property
    def database_url(self) -> str:
        return (
            "postgresql+asyncpg://"
            f"{self.postgres_user}"
            f":{self.postgres_password}"
            f"@{self.postgres_host}"
            f":{self.postgres_port}"
            f"/{self.postgres_db}"
        )

    @property
    def test_database_url(self) -> str:
        return f"{self.database_url}_test"

    @property
    def migration_database_url(self) -> str:
        return (
            self.database_url
            if self.environment == EnvEnum.production
            else f"{self.database_url}_migration"
        )

    @property
    def reset_db_on_startup(self) -> bool:
        return False

    @property
    def is_production(self) -> bool:
        return self.environment == EnvEnum.production

    @property
    def is_development(self) -> bool:
        return self.environment == EnvEnum.development


settings = Settings()
