"""Transport selector for choosing between stdio and HTTP transports.

Selects the appropriate A2A transport based on configuration:
- Stdio transport for local mode (no server URL configured)
- HTTP transport for server mode (server URL configured)
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Union

import structlog

if TYPE_CHECKING:
    from cleveragents.a2a.stdio_transport import A2aStdioTransport
    from cleveragents.a2a.transport import A2aHttpTransport

logger: structlog.stdlib.BoundLogger = structlog.get_logger(__name__)

# Type alias for transport union
A2aTransport = Union["A2aStdioTransport", "A2aHttpTransport"]


class TransportSelector:
    """Selects the appropriate A2A transport based on configuration.

    In local mode (no server URL), selects the stdio transport.
    In server mode (server URL configured), selects the HTTP transport.
    """

    @staticmethod
    def select(server_url: str | None = None) -> A2aTransport:
        """Select the appropriate transport.

        Args:
            server_url: The server URL for server mode, or None for local mode.

        Returns:
            An A2aStdioTransport for local mode, or A2aHttpTransport for server mode.
        """
        if not server_url:
            # Local mode: use stdio transport
            from cleveragents.a2a.stdio_transport import A2aStdioTransport

            logger.debug("a2a.transport_selector.selected_stdio")
            return A2aStdioTransport()
        else:
            # Server mode: use HTTP transport
            from cleveragents.a2a.transport import A2aHttpTransport

            logger.debug("a2a.transport_selector.selected_http", server_url=server_url)
            return A2aHttpTransport()


__all__ = [
    "A2aTransport",
    "TransportSelector",
]
