"""Configuration settings using pydantic-settings."""
import os
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import Literal


def get_env_file() -> str:
    """Get the appropriate env file based on ENVIRONMENT variable."""
    env = os.getenv("ENVIRONMENT", "development")
    env_file = f".env.{env}"
    
    # Check if env file exists, fallback to .env.development
    if not os.path.exists(env_file):
        env_file = ".env.development"
    
    return env_file


class Settings(BaseSettings):
    """Application settings loaded from environment variables."""

    # Database
    database_url: str = "postgresql+asyncpg://sibu:sibu@localhost:5432/sibu"

    # Google Maps (for server-side APIs)
    google_maps_api_key: str = ""
    google_maps_url_signing_secret: str = ""

    # Environment
    environment: Literal["development", "production", "testing"] = "development"
    debug: bool = False

    # Security
    admin_password: str = "admin"  # Default for development, override in .env
    secret_key: str = "insecure-secret-key-dev"  # Default for development, override in .env

    model_config = SettingsConfigDict(
        env_file=get_env_file(),
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="ignore",
    )


    # Global settings instance
    @property
    def get_database_url(self) -> str:
        url = self.database_url
        if url.startswith("postgres://"):
            url = url.replace("postgres://", "postgresql://", 1)
        return url

settings = Settings()
