"""
Transport factory for screenshare bot.

Provides factory function to create transport instances based on type.
"""

import logging
from typing import Optional, Any

from .base import BaseTransport

logger = logging.getLogger(__name__)

# Export public API
__all__ = ['BaseTransport', 'create_transport']

# IMPORTANT: Do NOT import transport adapters here!
# They must be lazy-loaded in create_transport() to avoid importing
# Pipecat when using LiveKit transport only.


def create_transport(
    transport_type: str,
    room_url: str,
    meeting_token: Optional[str],
    bot_name: str,
    camera_out_width: int = 1280,
    camera_out_height: int = 720,
    **kwargs: Any
) -> BaseTransport:
    """
    Create a transport instance based on type.

    Factory function that creates the appropriate transport adapter
    (Daily.co or LiveKit) based on the transport_type parameter.

    Args:
        transport_type: Transport backend ("daily" or "livekit")
        room_url: Room URL (Daily.co URL or LiveKit WebSocket URL)
        meeting_token: Optional authentication token
        bot_name: Bot display name
        camera_out_width: Video width in pixels (default 1280)
        camera_out_height: Video height in pixels (default 720)
        **kwargs: Transport-specific parameters:
            - Daily.co: audio_out_enabled, audio_in_enabled, camera_out_enabled,
                       vad_enabled, transcription_enabled
            - LiveKit: (additional params ignored for compatibility)

    Returns:
        BaseTransport instance (DailyTransportAdapter or LiveKitTransportAdapter)

    Raises:
        ValueError: If transport_type is not recognized

    Examples:
        # Create Daily.co transport
        transport = create_transport(
            transport_type="daily",
            room_url="https://domain.daily.co/room",
            meeting_token=None,
            bot_name="Screenshare Bot",
            audio_out_enabled=False,
            audio_in_enabled=False,
            camera_out_enabled=True
        )

        # Create LiveKit transport
        transport = create_transport(
            transport_type="livekit",
            room_url="wss://project.livekit.cloud",
            meeting_token=None,
            bot_name="Screenshare Bot"
        )
    """
    transport_type = transport_type.lower()

    logger.info(f"Creating {transport_type} transport for room: {room_url}")

    if transport_type == "daily":
        # Lazy import to avoid loading Pipecat when using LiveKit only
        from .daily_transport import DailyTransportAdapter
        return DailyTransportAdapter(
            room_url=room_url,
            meeting_token=meeting_token,
            bot_name=bot_name,
            camera_out_width=camera_out_width,
            camera_out_height=camera_out_height,
            **kwargs
        )
    elif transport_type == "livekit":
        # Lazy import to avoid loading LiveKit SDK when using Daily only
        from .livekit_transport import LiveKitTransportAdapter
        return LiveKitTransportAdapter(
            room_url=room_url,
            meeting_token=meeting_token,
            bot_name=bot_name,
            camera_out_width=camera_out_width,
            camera_out_height=camera_out_height,
            **kwargs
        )
    else:
        raise ValueError(
            f"Unknown transport type: {transport_type}. "
            f"Supported types: 'daily', 'livekit'"
        )
