"""
Comprehensive tests for the DuckDuckGo search engine.
Tests initialization, search functionality, error handling, and result parsing.

Note: These tests require the 'ddgs' package to be installed.
"""

import pytest

# Check if ddgs package is available
import importlib.util

DDGS_AVAILABLE = importlib.util.find_spec("ddgs") is not None


@pytest.mark.skipif(not DDGS_AVAILABLE, reason="ddgs package not installed")
class TestDuckDuckGoSearchEngineInit:
    """Tests for DuckDuckGo search engine initialization."""

    def test_init_default_parameters(self):
        """Test initialization with default parameters."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine()

        assert engine.max_results >= 10
        assert engine.is_public is True

    def test_init_custom_max_results(self):
        """Test initialization with custom max results."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine(max_results=25)
        assert engine.max_results >= 25

    def test_init_with_region(self):
        """Test initialization with specific region."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine(region="us-en")
        assert engine.region == "us-en"


@pytest.mark.skipif(not DDGS_AVAILABLE, reason="ddgs package not installed")
class TestDuckDuckGoSearchExecution:
    """Tests for DuckDuckGo search execution."""

    @pytest.fixture
    def ddg_engine(self):
        """Create a DuckDuckGo engine instance."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        return DuckDuckGoSearchEngine(max_results=10)

    def test_engine_initialization(self, ddg_engine):
        """Test that engine is properly initialized."""
        assert ddg_engine is not None
        assert ddg_engine.max_results >= 10


@pytest.mark.skipif(not DDGS_AVAILABLE, reason="ddgs package not installed")
class TestDuckDuckGoRegionSupport:
    """Tests for DuckDuckGo region/locale support."""

    def test_default_region(self):
        """Test default region configuration."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine()
        # Default region should be set or None
        assert hasattr(engine, "region")

    def test_custom_region(self):
        """Test custom region configuration."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine(region="de-de")
        assert engine.region == "de-de"


@pytest.mark.skipif(not DDGS_AVAILABLE, reason="ddgs package not installed")
class TestDuckDuckGoEngineType:
    """Tests for DuckDuckGo engine type identification."""

    def test_engine_type_set(self):
        """Test that engine type is properly set."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine()
        assert "duckduckgo" in engine.engine_type.lower()

    def test_engine_is_public(self):
        """Test that engine is marked as public."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine()
        assert engine.is_public is True

    def test_engine_is_generic(self):
        """Test that engine is marked as generic (not specialized)."""
        from local_deep_research.web_search_engines.engines.search_engine_ddg import (
            DuckDuckGoSearchEngine,
        )

        engine = DuckDuckGoSearchEngine()
        assert engine.is_generic is True


# Tests that can run without the ddgs package (just testing mock structure)
class TestDDGResponseFixtures:
    """Tests for DuckDuckGo response fixture structure."""

    def test_mock_response_structure(self, mock_ddg_response):
        """Test that mock response has correct structure."""
        assert isinstance(mock_ddg_response, list)
        assert len(mock_ddg_response) == 3

    def test_mock_response_has_required_fields(self, mock_ddg_response):
        """Test that mock response items have required fields."""
        for result in mock_ddg_response:
            assert "title" in result
            assert "href" in result
            assert "body" in result

    def test_mock_response_urls_valid(self, mock_ddg_response):
        """Test that URLs in mock response are valid."""
        for result in mock_ddg_response:
            assert result["href"].startswith("http")
