"""
Tests for search_utilities.py — format_links_to_markdown and format_findings
edge cases not covered by the existing test_search_utilities.py and
test_search_utilities_extended.py files.

Tests cover:
- format_links_to_markdown: None/empty URL handling, special characters, index
  aggregation for 'link' key fallback
- format_findings: iteration numbering, question_by_iteration edge cases,
  findings with search results that have errors, malformed finding structures
"""


# ---------------------------------------------------------------------------
# format_links_to_markdown — additional edge cases
# ---------------------------------------------------------------------------


class TestFormatLinksToMarkdownNoneUrls:
    """Tests for format_links_to_markdown with None/empty URLs."""

    def test_none_url_skipped(self):
        """Links with None url are skipped."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Valid", "url": "https://valid.com", "index": "1"},
            {"title": "None URL", "url": None, "index": "2"},
        ]

        result = format_links_to_markdown(links)

        assert "https://valid.com" in result
        assert "None URL" not in result

    def test_empty_string_url_skipped(self):
        """Links with empty string url are skipped."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Empty URL", "url": "", "index": "1"},
            {"title": "Good", "url": "https://good.com", "index": "2"},
        ]

        result = format_links_to_markdown(links)

        assert "https://good.com" in result
        assert "Empty URL" not in result

    def test_no_url_or_link_key_skipped(self):
        """Links with neither 'url' nor 'link' key are skipped."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "No URL key", "index": "1"},
        ]

        result = format_links_to_markdown(links)

        assert "No URL key" not in result


class TestFormatLinksToMarkdownSpecialChars:
    """Tests for format_links_to_markdown with special characters."""

    def test_url_with_query_params(self):
        """URLs with query parameters are preserved."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Search",
                "url": "https://example.com/search?q=hello+world&lang=en",
                "index": "1",
            }
        ]

        result = format_links_to_markdown(links)
        assert "https://example.com/search?q=hello+world&lang=en" in result

    def test_url_with_fragment(self):
        """URLs with fragments are preserved."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Section",
                "url": "https://example.com/page#section-2",
                "index": "1",
            }
        ]

        result = format_links_to_markdown(links)
        assert "https://example.com/page#section-2" in result

    def test_title_with_special_markdown_chars(self):
        """Titles with markdown-special chars are included."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {
                "title": "Results [2024] — *Important*",
                "url": "https://example.com",
                "index": "1",
            }
        ]

        result = format_links_to_markdown(links)
        assert "Results [2024]" in result


class TestFormatLinksToMarkdownIndexAggregation:
    """Tests for index aggregation in format_links_to_markdown."""

    def test_link_key_fallback_aggregates_indices(self):
        """'link' key fallback still aggregates indices for same URL."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "Same", "link": "https://same.com", "index": "1"},
            {"title": "Same", "link": "https://same.com", "index": "4"},
        ]

        result = format_links_to_markdown(links)
        assert result.count("https://same.com") == 1
        assert "1" in result
        assert "4" in result

    def test_mixed_url_and_link_keys(self):
        """Handles mix of 'url' and 'link' keys for same actual URL."""
        from local_deep_research.utilities.search_utilities import (
            format_links_to_markdown,
        )

        links = [
            {"title": "First", "url": "https://a.com", "index": "1"},
            {"title": "Second", "link": "https://b.com", "index": "2"},
        ]

        result = format_links_to_markdown(links)
        assert "https://a.com" in result
        assert "https://b.com" in result


# ---------------------------------------------------------------------------
# format_findings — additional edge cases
# ---------------------------------------------------------------------------


class TestFormatFindingsIterationNumbering:
    """Tests for iteration numbering in format_findings."""

    def test_single_iteration_with_questions(self):
        """Single iteration with questions formatted correctly."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        questions = {1: ["Q1", "Q2", "Q3"]}

        result = format_findings([], "Summary content", questions)

        assert "Iteration 1" in result
        assert "1. Q1" in result
        assert "2. Q2" in result
        assert "3. Q3" in result

    def test_non_sequential_iteration_numbers(self):
        """Non-sequential iteration numbers (e.g., 1, 3, 5) handled correctly."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        questions = {
            1: ["First iter Q"],
            3: ["Third iter Q"],
            5: ["Fifth iter Q"],
        }

        result = format_findings([], "Summary", questions)

        assert "Iteration 1" in result
        assert "Iteration 3" in result
        assert "Iteration 5" in result
        assert "Iteration 2" not in result

    def test_zero_iteration_key(self):
        """Iteration 0 (used by IterDRAG) is formatted."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        questions = {0: ["Sub-query A", "Sub-query B"]}

        result = format_findings([], "Summary", questions)

        assert "Iteration 0" in result
        assert "Sub-query A" in result


class TestFormatFindingsMalformedStructures:
    """Tests for malformed finding structures."""

    def test_finding_missing_content_key(self):
        """Finding without 'content' key uses default."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [{"phase": "Phase 1", "search_results": []}]

        result = format_findings(findings, "Summary", {})

        assert "No content available" in result

    def test_finding_missing_phase_key(self):
        """Finding without 'phase' key uses default."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [{"content": "Some content", "search_results": []}]

        result = format_findings(findings, "Summary", {})

        assert "Unknown Phase" in result
        assert "Some content" in result

    def test_finding_with_only_question_field(self):
        """Finding with only 'question' field displays it."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Custom",
                "content": "Answer text",
                "question": "How does X work?",
                "search_results": [],
            }
        ]

        result = format_findings(findings, "Summary", {})

        assert "How does X work?" in result
        assert "SEARCH QUESTION" in result

    def test_empty_questions_dict_shows_warning_section(self):
        """Empty questions dict (no iterations) omits section."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        result = format_findings([], "Summary", {})

        assert "SEARCH QUESTIONS BY ITERATION" not in result

    def test_findings_with_search_results_error(self):
        """Findings with non-list search_results handled gracefully."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Search",
                "content": "Content",
                "search_results": None,  # None instead of list
            }
        ]

        # Should not crash
        result = format_findings(findings, "Summary", {})
        assert "Content" in result
