"""AgenteFactory - Factory for creating agents with different profiles."""

from typing import Optional, Callable
from .imports import ConfigAgente, RetryStrategy, ExponentialBackoffStrategy


class AgenteFactory:
    """Factory for creating AgenteMaestroNASA instances with different profiles."""
    
    @staticmethod
    def quick(
        agent_class,
        name: str,
        handler: Optional[Callable] = None,
    ):
        """Create a quick/lightweight agent (minimal features).
        
        Profile characteristics:
        - Small queue (50 items)
        - Basic retry (1 attempt)
        - Circuit breaker DISABLED
        - Rate limiting DISABLED
        - Auditor DISABLED
        - Sandbox security DISABLED
        - Health probes DISABLED
        - Anti-starvation DISABLED
        - Basic metrics (always enabled)
        
        Args:
            agent_class: AgenteMaestroNASA or subclass
            name: Agent name
            handler: Optional external handler
            
        Returns:
            Configured agent instance
        """
        config = ConfigAgente(
            capacidad=50,
            max_reintentos=1,
            max_workers=1,
            circuit_breaker_threshold=100,
            rate_limit_tokens=1000,
            rate_limit_capacidad=10000,
            metricas_ventana_segundos=60,
            frecuencia_percepcion=5.0,
            # Disable optional components for lightweight profile
            enable_circuit_breaker=False,
            enable_rate_limiter=False,
            enable_auditor=False,
            enable_sandbox=False,
            enable_health_probes=False,
            enable_anti_starvation=False,
        )
        
        return agent_class(
            name=name,
            config=config,
            handler=handler,
            auto_persist=False
        )
    
    @staticmethod
    def standard(
        agent_class,
        name: str,
        handler: Optional[Callable] = None,
        retry_strategy: Optional[RetryStrategy] = None,
    ):
        """Create a standard agent (balanced features).
        
        Profile characteristics:
        - Medium queue (500 items)
        - Standard retry (3 attempts with exponential backoff)
        - Circuit breaker ENABLED
        - Rate limiting ENABLED
        - Auditor ENABLED
        - Sandbox security ENABLED
        - Health probes ENABLED
        - Anti-starvation ENABLED
        - Full metrics
        - Auto-persistence
        
        Args:
            agent_class: AgenteMaestroNASA or subclass
            name: Agent name
            handler: Optional external handler
            retry_strategy: Custom retry strategy (default: ExponentialBackoff)
            
        Returns:
            Configured agent instance
        """
        config = ConfigAgente(
            capacidad=500,
            max_reintentos=3,
            max_workers=4,
            circuit_breaker_threshold=10,  # Trip after 10 failures
            rate_limit_tokens=100,
            rate_limit_capacidad=1000,
            metricas_ventana_segundos=30,
            frecuencia_percepcion=2.0,
            # All enterprise features enabled for standard profile
            enable_circuit_breaker=True,
            enable_rate_limiter=True,
            enable_auditor=True,
            enable_sandbox=True,
            enable_health_probes=True,
            enable_anti_starvation=True,
        )
        
        return agent_class(
            name=name,
            config=config,
            handler=handler,
            auto_persist=True,
            retry=retry_strategy or ExponentialBackoffStrategy()
        )
    
    @staticmethod
    def industrial(
        agent_class,
        name: str,
        handler: Optional[Callable] = None,
        retry_strategy: Optional[RetryStrategy] = None,
    ):
        """Create an industrial/enterprise agent (maximum features).
        
        Profile characteristics:
        - Large queue (10000 items)
        - Aggressive retry (5 attempts with exponential backoff)
        - Sensitive circuit breaker (trip after 5 failures)
        - Strict rate limiting
        - Advanced metrics with small window
        - Continuous persistence
        - Anti-starvation enabled
        - Sandbox security enabled (whitelist mode)
        - Full health monitoring
        - ALL ENTERPRISE FEATURES ENABLED
        
        Args:
            agent_class: AgenteMaestroNASA or subclass
            name: Agent name
            handler: Optional external handler
            retry_strategy: Custom retry strategy (default: ExponentialBackoff)
            
        Returns:
            Configured agent instance
        """
        config = ConfigAgente(
            capacidad=10000,
            max_reintentos=5,
            max_workers=8,
            circuit_breaker_threshold=5,  # Trip after 5 failures (strict)
            rate_limit_tokens=10,
            rate_limit_capacidad=100,
            metricas_ventana_segundos=10,  # Tight window for real-time metrics
            frecuencia_percepcion=0.5,  # Continuous perception
            anti_inanicion_ratio_minimo=0.25,  # At least 25% low priority
            sandbox_habilitar_whitelist=True,  # Whitelist-only security
            max_memoria=10000,  # Large in-memory buffer
            max_registros_por_archivo=1000,  # Rotate logs frequently
            backpressure_threshold=0.7,  # Moderate backpressure
            # All enterprise features at maximum
            enable_circuit_breaker=True,
            enable_rate_limiter=True,
            enable_auditor=True,
            enable_sandbox=True,
            enable_health_probes=True,
            enable_anti_starvation=True,
        )
        
        return agent_class(
            name=name,
            config=config,
            handler=handler,
            auto_persist=True,
            retry=retry_strategy or ExponentialBackoffStrategy()
        )
    
    @staticmethod
    def custom(
        agent_class,
        name: str,
        config: ConfigAgente,
        handler: Optional[Callable] = None,
        retry_strategy: Optional[RetryStrategy] = None,
    ):
        """Create agent with fully custom configuration.
        
        Args:
            agent_class: AgenteMaestroNASA or subclass
            name: Agent name
            config: Custom ConfigAgente instance
            handler: Optional external handler
            retry_strategy: Custom retry strategy
            
        Returns:
            Configured agent instance
        """
        return agent_class(
            name=name,
            config=config,
            handler=handler,
            retry=retry_strategy
        )
