from typing import Optional
from urllib.parse import quote_plus
from pydantic_settings import BaseSettings, SettingsConfigDict

class Settings(BaseSettings):
    PROJECT_NAME: str = "CreditFair API"
    ENVIRONMENT: str = "development"

    # Observability
    LOG_LEVEL: str = "INFO"
    LOG_FILE_PATH: str = "logs/app.log"

    # DB Configuration (individual vars — avoids URL special-char encoding issues)
    DB_DRIVER: str = "mysql+mysqlconnector"
    DB_HOST: str = "127.0.0.1"
    DB_PORT: int = 3306
    DB_USER: str = "root"
    DB_PASSWORD: str = ""
    DB_NAME: str = "creditfair"

    @property
    def DATABASE_URL(self) -> str:
        return (
            f"{self.DB_DRIVER}://{quote_plus(self.DB_USER)}"
            f":{quote_plus(self.DB_PASSWORD)}"
            f"@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
        )
    
    # Security Configuration
    SECRET_KEY: str = "super_secret_key_change_in_production"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 1440  # 24 hours for development
    
    # Microsoft SSO
    MICROSOFT_CLIENT_ID: Optional[str] = None
    MICROSOFT_CLIENT_SECRET: Optional[str] = None
    MICROSOFT_REDIRECT_URI: Optional[str] = None
    MICROSOFT_TENANT: str = "common"
    
    # Frontend & Backend settings
    FRONTEND_APP_URL: str = "https://admin.creditfair.in"
    BACKEND_API_URL: str = "http://localhost:8000"    
    model_config = SettingsConfigDict(env_file=".env", env_file_encoding='utf-8', extra='ignore')

settings = Settings()
