from typing import Optional, List, Literal
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import Field

class GudbConfig(BaseSettings):
    """
    Global configuration for the gudb SDK.
    Values can be set via environment variables (e.g., GUDB_API_ENDPOINT).
    
    SECURITY GUARANTEE:
    - By default (mode="local", api_endpoint=None), gudb NEVER makes network calls
    - Remote/hybrid modes require EXPLICIT api_endpoint configuration
    - No telemetry, no cloud dependencies, no hardcoded URLs
    - Dashboard links are display-only and require explicit GUDB_DASHBOARD_URL
    """
    model_config = SettingsConfigDict(
        env_prefix="GUDB_",
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore"
    )

    # Core Settings
    api_endpoint: Optional[str] = Field(
        default=None,
        description=(
            "Remote AI evaluation endpoint (MUST be explicitly set for remote/hybrid modes). "
            "Default None ensures local-only operation with NO network calls. "
            "No hardcoded URLs, no telemetry, no cloud dependencies."
        )
    )

    dashboard_url: Optional[str] = Field(
        default=None,
        description=(
            "Dashboard URL for error message display (MUST be explicitly set). "
            "This is display-only and does NOT trigger network calls. "
            "No hardcoded URLs."
        )
    )
    
    enabled: bool = Field(default=True, description="Master switch for the SDK.")
    fail_open: bool = Field(
        default=True, 
        description="If True, queries pass if the AI backend is unreachable."
    )
    
    # Evaluation Mode
    mode: Literal["local", "remote", "hybrid"] = Field(
        default="local",
        description="Evaluation mode: 'local' (heuristics only), 'remote' (AI only), 'hybrid' (both)"
    )
    
    # Thresholds
    timeout_seconds: float = Field(default=0.1, description="Timeout for AI evaluation.")
    max_execution_time_ms: int = Field(default=2000, description="Max allowed DB execution time before termination.")
    
    # Heuristics
    enable_local_heuristics: bool = Field(
        default=True, 
        description="Enable zero-latency local checks for DELETE/DROP."
    )
    
    # Security
    redact_pii: bool = Field(default=True, description="Scrub SQL literals before sending to AI.")
    
    # Enterprise
    environment: str = Field(default="development", description="Current environment (prod/stage/dev).")
    service_name: str = Field(default="unknown-service", description="Name of the protected service.")
    
    # Risky Keywords to trigger AI logic
    risky_keywords: List[str] = Field(
        default_factory=lambda: ["DELETE", "UPDATE", "DROP", "ALTER", "TRUNCATE"]
    )

# Singleton instance for internal SDK usage
settings = GudbConfig()
