# Generated by Copilot
# Maintained by Dinesh Kumar C (https://github.com/Dinesh210805/blender-mcp)
# Forked from original work by Siddharth Ahuja (https://github.com/ahujasid/blender-mcp)

"""Configuration management for Blender MCP."""

import os
from typing import Optional
from dataclasses import dataclass
from pathlib import Path


@dataclass
class BlenderMCPConfig:
    """Configuration settings for Blender MCP server."""
    
    # Connection settings
    blender_host: str = "localhost"
    blender_port: int = 9876
    socket_timeout: float = 15.0
    connection_retry_attempts: int = 3
    connection_retry_delay: float = 1.0
    
    # Buffer and chunk sizes
    socket_buffer_size: int = 8192
    file_chunk_size: int = 8192
    
    # Feature flags
    polyhaven_enabled: bool = False
    hyper3d_enabled: bool = False
    sketchfab_enabled: bool = False
    
    # Security settings
    enable_code_execution: bool = True
    code_execution_timeout: float = 30.0
    max_file_size_mb: int = 100
    
    # Logging
    log_level: str = "INFO"
    log_format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    
    # Paths
    temp_dir: Optional[Path] = None
    
    @classmethod
    def from_env(cls) -> "BlenderMCPConfig":
        """Create configuration from environment variables."""
        return cls(
            blender_host=os.getenv("BLENDER_MCP_HOST", "localhost"),
            blender_port=int(os.getenv("BLENDER_MCP_PORT", "9876")),
            socket_timeout=float(os.getenv("BLENDER_MCP_SOCKET_TIMEOUT", "15.0")),
            connection_retry_attempts=int(os.getenv("BLENDER_MCP_RETRY_ATTEMPTS", "3")),
            connection_retry_delay=float(os.getenv("BLENDER_MCP_RETRY_DELAY", "1.0")),
            socket_buffer_size=int(os.getenv("BLENDER_MCP_BUFFER_SIZE", "8192")),
            file_chunk_size=int(os.getenv("BLENDER_MCP_CHUNK_SIZE", "8192")),
            polyhaven_enabled=os.getenv("BLENDER_MCP_POLYHAVEN_ENABLED", "false").lower() == "true",
            hyper3d_enabled=os.getenv("BLENDER_MCP_HYPER3D_ENABLED", "false").lower() == "true",
            sketchfab_enabled=os.getenv("BLENDER_MCP_SKETCHFAB_ENABLED", "false").lower() == "true",
            enable_code_execution=os.getenv("BLENDER_MCP_ENABLE_CODE_EXEC", "true").lower() == "true",
            code_execution_timeout=float(os.getenv("BLENDER_MCP_CODE_TIMEOUT", "30.0")),
            max_file_size_mb=int(os.getenv("BLENDER_MCP_MAX_FILE_SIZE_MB", "100")),
            log_level=os.getenv("BLENDER_MCP_LOG_LEVEL", "INFO"),
            log_format=os.getenv("BLENDER_MCP_LOG_FORMAT", "%(asctime)s - %(name)s - %(levelname)s - %(message)s"),
            temp_dir=Path(os.getenv("BLENDER_MCP_TEMP_DIR")) if os.getenv("BLENDER_MCP_TEMP_DIR") else None,
        )
    
    def get_temp_dir(self) -> Path:
        """Get the temporary directory path."""
        if self.temp_dir:
            return self.temp_dir
        return Path.cwd() / "temp"


# Global configuration instance
config = BlenderMCPConfig.from_env()
