"""
Tests for the search engine factory.

Tests cover:
- Creating parallel scientific search engine
- Handling missing API keys gracefully
"""

from unittest.mock import Mock, patch


class TestSearchEngineFactory:
    """Tests for the search engine factory functions."""

    def test_create_search_engine_parallel_scientific(self):
        """Create scientific parallel search engine."""
        from src.local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        mock_llm = Mock()

        # Minimal settings snapshot
        settings_snapshot = {
            "search.max_results": {"value": 10},
        }

        result = create_search_engine(
            engine_name="parallel_scientific",
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
        )

        # Should return a ParallelSearchEngine
        assert result is not None
        assert "ParallelSearchEngine" in type(result).__name__

    def test_create_search_engine_parallel(self):
        """Create standard parallel search engine."""
        from src.local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        mock_llm = Mock()

        settings_snapshot = {
            "search.max_results": {"value": 10},
        }

        result = create_search_engine(
            engine_name="parallel",
            llm=mock_llm,
            settings_snapshot=settings_snapshot,
        )

        assert result is not None
        assert "ParallelSearchEngine" in type(result).__name__

    def test_create_search_engine_missing_api_key(self):
        """Handle missing API key gracefully."""
        from src.local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        mock_llm = Mock()

        # Settings for an engine that requires API key, but key is missing
        settings_snapshot = {
            "search.max_results": {"value": 10},
            "search.engine.web.brave": {
                "module_path": ".engines.search_engine_brave",
                "class_name": "BraveSearchEngine",
                "requires_api_key": True,
                "default_params": {},
            },
        }

        # Mock the search_config to return config requiring API key
        with patch(
            "src.local_deep_research.web_search_engines.search_engine_factory.search_config"
        ) as mock_config:
            mock_config.return_value = {
                "brave": {
                    "module_path": ".engines.search_engine_brave",
                    "class_name": "BraveSearchEngine",
                    "requires_api_key": True,
                    "default_params": {},
                }
            }

            result = create_search_engine(
                engine_name="brave",
                llm=mock_llm,
                settings_snapshot=settings_snapshot,
            )

            # Should return None when API key is required but missing
            assert result is None

    def test_create_search_engine_unknown_engine(self):
        """Unknown engine name should be handled gracefully."""
        from src.local_deep_research.web_search_engines.search_engine_factory import (
            create_search_engine,
        )

        mock_llm = Mock()

        # Settings snapshot without the unknown engine
        settings_snapshot = {
            "search.max_results": {"value": 10},
        }

        with patch(
            "src.local_deep_research.web_search_engines.search_engine_factory.search_config"
        ) as mock_config:
            mock_config.return_value = {}  # Empty config

            result = create_search_engine(
                engine_name="totally_unknown_engine",
                llm=mock_llm,
                settings_snapshot=settings_snapshot,
            )

            # Should return None for unknown engine
            assert result is None
