"""
Builder utilities for cleaner, more ergonomic API usage.

These builders provide a fluent interface for constructing agents,
workspaces, and configurations with sensible defaults.
"""

from typing import Optional, Callable, Any
from synqed.workspace_engine import AgentInfo, WorkspaceConfig, WorkspaceEngine
from synqed.protocols import ProtocolType, ProtocolConfig
from synqed.scheduler import SchedulerConfig, SchedulingStrategy


class WorkspaceBuilder:
    """Fluent builder for creating workspaces with sensible defaults."""
    
    def __init__(self, name: str):
        self._name = name
        self._description: str = ""
        self._protocol_type: ProtocolType = ProtocolType.CONSENSUS
        self._protocol_config: Optional[ProtocolConfig] = None
        self._scheduler_config: Optional[SchedulerConfig] = None
        self._max_rounds: int = 5
        self._max_messages: int = 100
        self._timeout_seconds: int = 300
        self._on_message: Optional[Callable] = None
        self._on_phase_change: Optional[Callable] = None
        self._on_round_end: Optional[Callable] = None
    
    def with_description(self, description: str) -> "WorkspaceBuilder":
        """Set workspace description."""
        self._description = description
        return self
    
    def with_protocol(self, protocol_type: ProtocolType) -> "WorkspaceBuilder":
        """Set the protocol type."""
        self._protocol_type = protocol_type
        return self
    
    def with_consensus(self, max_rounds: int = 5, convergence: float = 0.8) -> "WorkspaceBuilder":
        """Configure for consensus protocol."""
        self._protocol_type = ProtocolType.CONSENSUS
        self._protocol_config = ProtocolConfig(
            max_rounds=max_rounds,
            convergence_threshold=convergence,
        )
        return self
    
    def with_debate(self, max_rounds: int = 3) -> "WorkspaceBuilder":
        """Configure for debate protocol."""
        self._protocol_type = ProtocolType.DEBATE
        self._protocol_config = ProtocolConfig(max_rounds=max_rounds)
        return self
    
    def with_critique_and_revise(self, max_rounds: int = 3) -> "WorkspaceBuilder":
        """Configure for critique-and-revise protocol."""
        self._protocol_type = ProtocolType.CRITIQUE_AND_REVISE
        self._protocol_config = ProtocolConfig(max_rounds=max_rounds)
        return self
    
    def with_max_messages(self, max_messages: int) -> "WorkspaceBuilder":
        """Set maximum messages."""
        self._max_messages = max_messages
        return self
    
    def with_timeout(self, seconds: int) -> "WorkspaceBuilder":
        """Set timeout in seconds."""
        self._timeout_seconds = seconds
        return self
    
    def on_message(self, callback: Callable) -> "WorkspaceBuilder":
        """Set message callback."""
        self._on_message = callback
        return self
    
    def on_phase_change(self, callback: Callable) -> "WorkspaceBuilder":
        """Set phase change callback."""
        self._on_phase_change = callback
        return self
    
    def on_round_end(self, callback: Callable) -> "WorkspaceBuilder":
        """Set round end callback."""
        self._on_round_end = callback
        return self
    
    def build(self) -> WorkspaceEngine:
        """Build the workspace engine."""
        config = WorkspaceConfig(
            name=self._name,
            description=self._description,
            protocol_type=self._protocol_type,
            protocol_config=self._protocol_config,
            scheduler_config=self._scheduler_config,
            max_rounds=self._max_rounds,
            max_messages=self._max_messages,
            timeout_seconds=self._timeout_seconds,
            on_message=self._on_message,
            on_phase_change=self._on_phase_change,
            on_round_end=self._on_round_end,
        )
        return WorkspaceEngine(config)


def create_consensus_workspace(
    name: str,
    message_handler: Optional[Callable] = None,
    max_rounds: int = 5,
    convergence: float = 0.8,
) -> WorkspaceEngine:
    """
    Quick consensus workspace creation.
    
    Example:
        workspace = create_consensus_workspace("Team Planning", 
                                              message_handler=print_message)
    """
    config = WorkspaceConfig(
        name=name,
        protocol_type=ProtocolType.CONSENSUS,
        protocol_config=ProtocolConfig(
            max_rounds=max_rounds,
            convergence_threshold=convergence,
        ),
        on_message=message_handler,
    )
    return WorkspaceEngine(config)


def create_debate_workspace(
    name: str,
    message_handler: Optional[Callable] = None,
    max_rounds: int = 3,
) -> WorkspaceEngine:
    """
    Quick debate workspace creation.
    
    Example:
        workspace = create_debate_workspace("Policy Debate", 
                                           message_handler=print_message)
    """
    config = WorkspaceConfig(
        name=name,
        protocol_type=ProtocolType.DEBATE,
        protocol_config=ProtocolConfig(max_rounds=max_rounds),
        on_message=message_handler,
    )
    return WorkspaceEngine(config)


def create_critique_workspace(
    name: str,
    message_handler: Optional[Callable] = None,
    max_rounds: int = 3,
) -> WorkspaceEngine:
    """
    Quick critique-and-revise workspace creation.
    
    Example:
        workspace = create_critique_workspace("Design Review",
                                             message_handler=print_message)
    """
    config = WorkspaceConfig(
        name=name,
        protocol_type=ProtocolType.CRITIQUE_AND_REVISE,
        protocol_config=ProtocolConfig(max_rounds=max_rounds),
        on_message=message_handler,
    )
    return WorkspaceEngine(config)

