"""
Tests for the FindingsRepository class.

Tests cover:
- Initialization
- Adding findings
- Getting findings
- Clearing findings
- Format links helper
"""

from unittest.mock import Mock


class TestFormatLinks:
    """Tests for format_links helper function."""

    def test_format_empty_links(self):
        """format_links handles empty list."""
        from local_deep_research.advanced_search_system.findings.repository import (
            format_links,
        )

        result = format_links([])

        assert result == ""

    def test_format_single_link(self):
        """format_links formats single link."""
        from local_deep_research.advanced_search_system.findings.repository import (
            format_links,
        )

        links = [{"title": "Test Title", "url": "https://test.com"}]
        result = format_links(links)

        assert "1. Test Title" in result
        assert "URL: https://test.com" in result

    def test_format_multiple_links(self):
        """format_links formats multiple links with numbers."""
        from local_deep_research.advanced_search_system.findings.repository import (
            format_links,
        )

        links = [
            {"title": "Title 1", "url": "https://a.com"},
            {"title": "Title 2", "url": "https://b.com"},
        ]
        result = format_links(links)

        assert "1. Title 1" in result
        assert "2. Title 2" in result
        assert "URL: https://a.com" in result
        assert "URL: https://b.com" in result


class TestFindingsRepositoryInit:
    """Tests for FindingsRepository initialization."""

    def test_init_stores_model(self):
        """Repository stores the model reference."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        assert repo.model is mock_model

    def test_init_creates_empty_findings(self):
        """Repository initializes with empty findings dict."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        assert repo.findings == {}

    def test_init_creates_empty_documents(self):
        """Repository initializes with empty documents list."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        assert repo.documents == []

    def test_init_creates_empty_questions_by_iteration(self):
        """Repository initializes with empty questions_by_iteration dict."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        assert repo.questions_by_iteration == {}


class TestAddFinding:
    """Tests for add_finding method."""

    def test_add_finding_with_dict(self):
        """add_finding adds dict finding."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        finding = {
            "phase": "Search",
            "content": "Test content",
            "question": "Test query",
        }
        repo.add_finding("test query", finding)

        assert len(repo.get_findings("test query")) == 1

    def test_add_finding_with_string(self):
        """add_finding converts string to dict."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        repo.add_finding("test query", "String finding content")

        findings = repo.get_findings("test query")
        assert len(findings) == 1
        assert findings[0]["content"] == "String finding content"
        assert findings[0]["phase"] == "Synthesis"

    def test_add_multiple_findings(self):
        """add_finding allows multiple findings per query."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        repo.add_finding("query", {"content": "Finding 1"})
        repo.add_finding("query", {"content": "Finding 2"})

        assert len(repo.get_findings("query")) == 2

    def test_add_final_synthesis_finding(self):
        """add_finding stores synthesis separately for final phase."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        finding = {"phase": "Final synthesis", "content": "Final content"}
        repo.add_finding("query", finding)

        # Should also have synthesis stored
        synthesis = repo.get_findings("query_synthesis")
        assert len(synthesis) == 1


class TestGetFindings:
    """Tests for get_findings method."""

    def test_get_findings_for_existing_query(self):
        """get_findings returns findings for existing query."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        repo.add_finding("test query", {"content": "Test"})

        result = repo.get_findings("test query")

        assert len(result) == 1

    def test_get_findings_for_nonexistent_query(self):
        """get_findings returns empty list for nonexistent query."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        result = repo.get_findings("nonexistent")

        assert result == []


class TestClearFindings:
    """Tests for clear_findings method."""

    def test_clear_findings_removes_query(self):
        """clear_findings removes findings for query."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        repo.add_finding("query", {"content": "Test"})
        repo.clear_findings("query")

        assert repo.get_findings("query") == []

    def test_clear_findings_nonexistent_query(self):
        """clear_findings handles nonexistent query gracefully."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        # Should not raise
        repo.clear_findings("nonexistent")

    def test_clear_findings_preserves_other_queries(self):
        """clear_findings only clears specified query."""
        from local_deep_research.advanced_search_system.findings.repository import (
            FindingsRepository,
        )

        mock_model = Mock()
        repo = FindingsRepository(mock_model)

        repo.add_finding("query1", {"content": "Test 1"})
        repo.add_finding("query2", {"content": "Test 2"})
        repo.clear_findings("query1")

        assert repo.get_findings("query1") == []
        assert len(repo.get_findings("query2")) == 1
