"""
Tests for uncovered code paths in FullSearchResults.

Targets:
- _get_full_content() method
- SSRF validation blocking all URLs
- check_urls with null LLM
- run() with SSRF-blocked URLs returning results without full content
"""

from unittest.mock import Mock, patch


class TestGetFullContent:
    """Tests for _get_full_content method."""

    def _make_engine(self):
        from local_deep_research.web_search_engines.engines.full_search import (
            FullSearchResults,
        )

        mock_llm = Mock()
        mock_web_search = Mock()
        engine = FullSearchResults(llm=mock_llm, web_search=mock_web_search)
        return engine

    def test_get_full_content_all_urls_valid(self):
        """_get_full_content fetches content for valid URLs."""
        engine = self._make_engine()

        items = [
            {"link": "https://example.com/1", "title": "R1"},
            {"link": "https://example.com/2", "title": "R2"},
        ]

        with patch(
            "local_deep_research.web_search_engines.engines.full_search.validate_url",
            return_value=True,
        ):
            with patch(
                "local_deep_research.web_search_engines.engines.full_search.batch_fetch_and_extract",
                return_value={
                    "https://example.com/1": "Content 1",
                    "https://example.com/2": "Content 2",
                },
            ):
                result = engine._get_full_content(items)

        assert result[0]["full_content"] == "Content 1"
        assert result[1]["full_content"] == "Content 2"

    def test_get_full_content_no_valid_urls(self):
        """_get_full_content returns items with None content when all URLs fail SSRF."""
        engine = self._make_engine()

        items = [
            {"link": "http://192.168.1.1/internal", "title": "R1"},
            {"link": "http://10.0.0.1/secret", "title": "R2"},
        ]

        with patch(
            "local_deep_research.web_search_engines.engines.full_search.validate_url",
            return_value=False,
        ):
            result = engine._get_full_content(items)

        assert result[0]["full_content"] is None
        assert result[1]["full_content"] is None

    def test_get_full_content_fetcher_exception(self):
        """_get_full_content returns None content on fetcher exception."""
        engine = self._make_engine()

        items = [{"link": "https://example.com/1", "title": "R1"}]

        with patch(
            "local_deep_research.web_search_engines.engines.full_search.validate_url",
            return_value=True,
        ):
            with patch(
                "local_deep_research.web_search_engines.engines.full_search.batch_fetch_and_extract",
                side_effect=Exception("Network error"),
            ):
                result = engine._get_full_content(items)

        assert result[0]["full_content"] is None

    def test_get_full_content_item_without_link(self):
        """_get_full_content handles items without link key."""
        engine = self._make_engine()

        items = [
            {"title": "No link item"},
            {"link": "https://example.com/1", "title": "Has link"},
        ]

        with patch(
            "local_deep_research.web_search_engines.engines.full_search.validate_url",
            return_value=True,
        ):
            with patch(
                "local_deep_research.web_search_engines.engines.full_search.batch_fetch_and_extract",
                return_value={
                    "https://example.com/1": "Content",
                },
            ):
                result = engine._get_full_content(items)

        assert result[0]["full_content"] is None  # No link -> None
        assert result[1]["full_content"] == "Content"

    def test_get_full_content_mixed_ssrf_validation(self):
        """_get_full_content handles mix of valid and SSRF-blocked URLs."""
        engine = self._make_engine()

        items = [
            {"link": "https://example.com/ok", "title": "Good"},
            {"link": "http://192.168.1.1/bad", "title": "Bad"},
        ]

        def mock_validate(url):
            return "example.com" in url

        with patch(
            "local_deep_research.web_search_engines.engines.full_search.validate_url",
            side_effect=mock_validate,
        ):
            with patch(
                "local_deep_research.web_search_engines.engines.full_search.batch_fetch_and_extract",
                return_value={
                    "https://example.com/ok": "Content",
                },
            ):
                result = engine._get_full_content(items)

        assert result[0]["full_content"] == "Content"
        assert result[1]["full_content"] is None


class TestCheckUrlsNullLlm:
    """Tests for check_urls with null LLM."""

    def test_check_urls_null_llm_returns_results_unchanged(self):
        """check_urls returns results unchanged when LLM is None."""
        from local_deep_research.web_search_engines.engines.full_search import (
            FullSearchResults,
        )

        mock_web_search = Mock()
        engine = FullSearchResults(llm=None, web_search=mock_web_search)

        results = [
            {"link": "https://example.com/1", "title": "Result 1"},
            {"link": "https://example.com/2", "title": "Result 2"},
        ]

        filtered = engine.check_urls(results, "test query")

        assert filtered == results
        assert len(filtered) == 2


class TestRunSsrfBlocking:
    """Tests for run() SSRF blocking paths."""

    def test_run_all_urls_ssrf_blocked(self):
        """run() returns results without full content when all URLs fail SSRF."""
        from local_deep_research.web_search_engines.engines.full_search import (
            FullSearchResults,
        )

        mock_llm = Mock()
        mock_web_search = Mock()
        mock_web_search.invoke.return_value = [
            {"link": "http://192.168.1.1/internal", "title": "Internal"},
        ]

        with patch(
            "local_deep_research.web_search_engines.engines.full_search.QUALITY_CHECK_DDG_URLS",
            False,
        ):
            with patch(
                "local_deep_research.web_search_engines.engines.full_search.validate_url",
                return_value=False,
            ):
                engine = FullSearchResults(
                    llm=mock_llm, web_search=mock_web_search
                )
                results = engine.run("test query")

        assert len(results) == 1
        assert results[0]["full_content"] is None

    def test_run_with_batch_fetch(self):
        """run() uses batch_fetch_and_extract and attaches content to results."""
        from local_deep_research.web_search_engines.engines.full_search import (
            FullSearchResults,
        )

        mock_llm = Mock()
        mock_web_search = Mock()
        mock_web_search.invoke.return_value = [
            {"link": "https://example.com/1", "title": "R1"},
        ]

        with (
            patch(
                "local_deep_research.web_search_engines.engines.full_search.QUALITY_CHECK_DDG_URLS",
                False,
            ),
            patch(
                "local_deep_research.web_search_engines.engines.full_search.validate_url",
                return_value=True,
            ),
            patch(
                "local_deep_research.web_search_engines.engines.full_search.batch_fetch_and_extract",
                return_value={
                    "https://example.com/1": "Page content",
                },
            ),
        ):
            engine = FullSearchResults(llm=mock_llm, web_search=mock_web_search)
            results = engine.run("test query")

        assert len(results) == 1
        assert results[0]["full_content"] == "Page content"
