"""
Behavioral tests for llm/providers/auto_discovery module.

Tests ProviderInfo display name generation, to_dict serialization,
and ProviderDiscovery singleton pattern.
"""


class TestProviderInfoInit:
    """Tests for ProviderInfo initialization."""

    def _make_provider_class(self, **attrs):
        """Helper to create a minimal provider class for testing."""

        class MockProvider:
            provider_name = attrs.get("provider_name", "TestProvider")
            provider_key = attrs.get("provider_key", "TEST")

            @classmethod
            def requires_auth_for_models(cls):
                return attrs.get("requires_auth", True)

        for key, value in attrs.items():
            if key not in ("provider_name", "provider_key", "requires_auth"):
                setattr(MockProvider, key, value)
        return MockProvider

    def test_stores_provider_class(self):
        """Stores reference to provider class."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        cls = self._make_provider_class()
        info = ProviderInfo(cls)
        assert info.provider_class is cls

    def test_extracts_provider_key(self):
        """Extracts provider_key from class."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        cls = self._make_provider_class(provider_key="MYKEY")
        info = ProviderInfo(cls)
        assert info.provider_key == "MYKEY"

    def test_extracts_provider_name(self):
        """Extracts provider_name from class."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        cls = self._make_provider_class(provider_name="MyService")
        info = ProviderInfo(cls)
        assert info.provider_name == "MyService"

    def test_default_is_cloud_true(self):
        """Default is_cloud is True."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        cls = self._make_provider_class()
        info = ProviderInfo(cls)
        assert info.is_cloud is True

    def test_local_provider(self):
        """Reads is_cloud=False for local providers."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        cls = self._make_provider_class(is_cloud=False)
        info = ProviderInfo(cls)
        assert info.is_cloud is False

    def test_requires_auth_from_method(self):
        """Reads requires_auth_for_models from class method."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        cls = self._make_provider_class(requires_auth=False)
        info = ProviderInfo(cls)
        assert info.requires_auth_for_models is False

    def test_display_name_generated(self):
        """Display name is generated on init."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        cls = self._make_provider_class(provider_name="MyService")
        info = ProviderInfo(cls)
        assert info.display_name is not None
        assert isinstance(info.display_name, str)
        assert len(info.display_name) > 0


class TestProviderInfoDisplayName:
    """Tests for ProviderInfo._generate_display_name."""

    def _make_info(self, **attrs):
        """Helper to create ProviderInfo with given attributes."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        class MockProvider:
            provider_name = attrs.get("provider_name", "TestProvider")
            provider_key = attrs.get("provider_key", "TEST")

            @classmethod
            def requires_auth_for_models(cls):
                return True

        for key, value in attrs.items():
            if key not in ("provider_name", "provider_key"):
                setattr(MockProvider, key, value)
        return ProviderInfo(MockProvider)

    def test_includes_provider_name(self):
        """Display name includes provider name."""
        info = self._make_info(provider_name="SuperAI")
        assert "SuperAI" in info.display_name

    def test_includes_cloud_indicator(self):
        """Display name includes cloud indicator for cloud providers."""
        info = self._make_info(is_cloud=True)
        assert "Cloud" in info.display_name

    def test_includes_local_indicator(self):
        """Display name includes local indicator for local providers."""
        info = self._make_info(is_cloud=False)
        assert "Local" in info.display_name


class TestProviderInfoToDict:
    """Tests for ProviderInfo.to_dict method."""

    def _make_info(self, **attrs):
        """Helper to create ProviderInfo with given attributes."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )

        class MockProvider:
            provider_name = attrs.get("provider_name", "TestProvider")
            provider_key = attrs.get("provider_key", "TEST")

            @classmethod
            def requires_auth_for_models(cls):
                return True

        for key, value in attrs.items():
            if key not in ("provider_name", "provider_key"):
                setattr(MockProvider, key, value)
        return ProviderInfo(MockProvider)

    def test_returns_dict(self):
        """to_dict returns a dictionary."""
        info = self._make_info()
        assert isinstance(info.to_dict(), dict)

    def test_has_value_key(self):
        """to_dict has 'value' key with provider key."""
        info = self._make_info(provider_key="MYTEST")
        result = info.to_dict()
        assert result["value"] == "MYTEST"

    def test_has_label_key(self):
        """to_dict has 'label' key with display name."""
        info = self._make_info()
        result = info.to_dict()
        assert "label" in result
        assert isinstance(result["label"], str)

    def test_has_is_cloud(self):
        """to_dict has 'is_cloud' key."""
        info = self._make_info(is_cloud=True)
        result = info.to_dict()
        assert result["is_cloud"] is True


class TestProviderDiscoverySingleton:
    """Tests for ProviderDiscovery singleton pattern."""

    def test_is_singleton(self):
        """Multiple instances return same object."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderDiscovery,
        )

        a = ProviderDiscovery()
        b = ProviderDiscovery()
        assert a is b

    def test_has_discover_providers_method(self):
        """Has discover_providers method."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderDiscovery,
        )

        assert hasattr(ProviderDiscovery(), "discover_providers")

    def test_has_get_provider_info_method(self):
        """Has get_provider_info method."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderDiscovery,
        )

        assert hasattr(ProviderDiscovery(), "get_provider_info")

    def test_has_get_provider_options_method(self):
        """Has get_provider_options method."""
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderDiscovery,
        )

        assert hasattr(ProviderDiscovery(), "get_provider_options")
