"""
Unit tests for transport factory function.

Tests the create_transport factory function with mocking to ensure
proper instantiation of Daily and LiveKit transport adapters.
"""

import pytest
from unittest.mock import Mock, patch, MagicMock

from transports import create_transport, BaseTransport
from transports.daily_transport import DailyTransportAdapter
from transports.livekit_transport import LiveKitTransportAdapter


class TestTransportFactory:
    """Test transport factory function."""

    @patch('transports.daily_transport.DailyTransport')
    def test_create_daily_transport(self, mock_daily_transport):
        """Test creating Daily.co transport."""
        transport = create_transport(
            transport_type="daily",
            room_url="https://test.daily.co/room",
            meeting_token=None,
            bot_name="Test Bot"
        )

        # Verify correct type
        assert isinstance(transport, DailyTransportAdapter)
        assert isinstance(transport, BaseTransport)

        # Verify configuration
        assert transport.room_url == "https://test.daily.co/room"
        assert transport.meeting_token is None
        assert transport.bot_name == "Test Bot"

    def test_create_livekit_transport(self):
        """Test creating LiveKit transport."""
        transport = create_transport(
            transport_type="livekit",
            room_url="wss://test.livekit.cloud",
            meeting_token="test-token",
            bot_name="Test Bot"
        )

        # Verify correct type
        assert isinstance(transport, LiveKitTransportAdapter)
        assert isinstance(transport, BaseTransport)

        # Verify configuration
        assert transport.room_url == "wss://test.livekit.cloud"
        assert transport.meeting_token == "test-token"
        assert transport.bot_name == "Test Bot"

    def test_unknown_transport_type(self):
        """Test error on unknown transport type."""
        with pytest.raises(ValueError, match="Unknown transport type"):
            create_transport(
                transport_type="unknown",
                room_url="https://test.com",
                meeting_token=None,
                bot_name="Test Bot"
            )

    def test_case_insensitive_transport_type(self):
        """Test that transport type is case insensitive."""
        # Test uppercase
        transport = create_transport(
            transport_type="LIVEKIT",
            room_url="wss://test.livekit.cloud",
            meeting_token="test-token",
            bot_name="Test Bot"
        )
        assert isinstance(transport, LiveKitTransportAdapter)

        # Test mixed case
        transport = create_transport(
            transport_type="LiveKit",
            room_url="wss://test.livekit.cloud",
            meeting_token="test-token",
            bot_name="Test Bot"
        )
        assert isinstance(transport, LiveKitTransportAdapter)

    @patch('transports.daily_transport.DailyTransport')
    def test_factory_returns_base_transport_interface(self, mock_daily_transport):
        """Test that all transports implement BaseTransport."""
        for transport_type in ["daily", "livekit"]:
            transport = create_transport(
                transport_type=transport_type,
                room_url="https://test.com",
                meeting_token=None,
                bot_name="Test Bot"
            )

            # Check interface methods exist
            assert hasattr(transport, 'join_room')
            assert hasattr(transport, 'send_image')
            assert hasattr(transport, 'cleanup')

            # Check methods are callable
            assert callable(transport.join_room)
            assert callable(transport.send_image)
            assert callable(transport.cleanup)

    def test_custom_dimensions(self):
        """Test that custom video dimensions are passed through."""
        transport = create_transport(
            transport_type="livekit",
            room_url="wss://test.livekit.cloud",
            meeting_token="test-token",
            bot_name="Test Bot",
            camera_out_width=1920,
            camera_out_height=1080
        )

        assert transport.width == 1920
        assert transport.height == 1080

    def test_default_dimensions(self):
        """Test that default video dimensions are 1280x720."""
        transport = create_transport(
            transport_type="livekit",
            room_url="wss://test.livekit.cloud",
            meeting_token="test-token",
            bot_name="Test Bot"
        )

        assert transport.width == 1280
        assert transport.height == 720

    @patch('transports.daily_transport.DailyTransport')
    def test_daily_specific_kwargs(self, mock_daily_transport):
        """Test that Daily-specific kwargs are passed through."""
        transport = create_transport(
            transport_type="daily",
            room_url="https://test.daily.co/room",
            meeting_token=None,
            bot_name="Test Bot",
            audio_out_enabled=True,
            audio_in_enabled=True,
            camera_out_enabled=True,
            vad_enabled=True,
            transcription_enabled=True
        )

        # Verify transport was created
        assert isinstance(transport, DailyTransportAdapter)

    def test_livekit_ignores_daily_kwargs(self):
        """Test that LiveKit transport ignores Daily-specific kwargs."""
        # Should not raise error even with Daily-specific params
        transport = create_transport(
            transport_type="livekit",
            room_url="wss://test.livekit.cloud",
            meeting_token="test-token",
            bot_name="Test Bot",
            audio_out_enabled=True,
            vad_enabled=True,
            transcription_enabled=True
        )

        assert isinstance(transport, LiveKitTransportAdapter)
