"""
Tests for advanced_search_system/findings/base_findings.py

Tests cover:
- BaseFindingsRepository abstract class
- Initialization
- Abstract method requirements
"""

from unittest.mock import Mock

import pytest


class TestBaseFindingsRepositoryInit:
    """Tests for BaseFindingsRepository initialization."""

    def test_init_stores_model(self):
        """Test that model is stored."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        class ConcreteRepository(BaseFindingsRepository):
            def add_finding(self, query, finding):
                pass

            def get_findings(self, query):
                return []

            def clear_findings(self, query):
                pass

            def synthesize_findings(
                self, query, sub_queries, findings, accumulated_knowledge
            ):
                return ""

        mock_model = Mock()
        repo = ConcreteRepository(mock_model)

        assert repo.model is mock_model

    def test_init_creates_empty_findings_dict(self):
        """Test that findings dict is initialized empty."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        class ConcreteRepository(BaseFindingsRepository):
            def add_finding(self, query, finding):
                pass

            def get_findings(self, query):
                return []

            def clear_findings(self, query):
                pass

            def synthesize_findings(
                self, query, sub_queries, findings, accumulated_knowledge
            ):
                return ""

        repo = ConcreteRepository(Mock())

        assert repo.findings == {}


class TestBaseFindingsRepositoryAbstract:
    """Tests for BaseFindingsRepository abstract methods."""

    def test_cannot_instantiate_directly(self):
        """Test that BaseFindingsRepository cannot be instantiated."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        with pytest.raises(TypeError):
            BaseFindingsRepository(Mock())

    def test_requires_add_finding(self):
        """Test that add_finding must be implemented."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        class IncompleteRepository(BaseFindingsRepository):
            def get_findings(self, query):
                return []

            def clear_findings(self, query):
                pass

            def synthesize_findings(
                self, query, sub_queries, findings, accumulated_knowledge
            ):
                return ""

        with pytest.raises(TypeError):
            IncompleteRepository(Mock())

    def test_requires_get_findings(self):
        """Test that get_findings must be implemented."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        class IncompleteRepository(BaseFindingsRepository):
            def add_finding(self, query, finding):
                pass

            def clear_findings(self, query):
                pass

            def synthesize_findings(
                self, query, sub_queries, findings, accumulated_knowledge
            ):
                return ""

        with pytest.raises(TypeError):
            IncompleteRepository(Mock())

    def test_requires_clear_findings(self):
        """Test that clear_findings must be implemented."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        class IncompleteRepository(BaseFindingsRepository):
            def add_finding(self, query, finding):
                pass

            def get_findings(self, query):
                return []

            def synthesize_findings(
                self, query, sub_queries, findings, accumulated_knowledge
            ):
                return ""

        with pytest.raises(TypeError):
            IncompleteRepository(Mock())

    def test_requires_synthesize_findings(self):
        """Test that synthesize_findings must be implemented."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        class IncompleteRepository(BaseFindingsRepository):
            def add_finding(self, query, finding):
                pass

            def get_findings(self, query):
                return []

            def clear_findings(self, query):
                pass

        with pytest.raises(TypeError):
            IncompleteRepository(Mock())


class TestConcreteImplementation:
    """Tests for a concrete implementation of BaseFindingsRepository."""

    def test_concrete_implementation_works(self):
        """Test that concrete implementation can be instantiated."""
        from local_deep_research.advanced_search_system.findings.base_findings import (
            BaseFindingsRepository,
        )

        class ConcreteRepository(BaseFindingsRepository):
            def add_finding(self, query, finding):
                if query not in self.findings:
                    self.findings[query] = []
                self.findings[query].append(finding)

            def get_findings(self, query):
                return self.findings.get(query, [])

            def clear_findings(self, query):
                if query in self.findings:
                    del self.findings[query]

            def synthesize_findings(
                self, query, sub_queries, findings, accumulated_knowledge
            ):
                return f"Synthesized: {len(findings)} findings"

        repo = ConcreteRepository(Mock())

        # Test add_finding
        repo.add_finding("test query", "finding 1")
        assert repo.get_findings("test query") == ["finding 1"]

        # Test clear_findings
        repo.clear_findings("test query")
        assert repo.get_findings("test query") == []

        # Test synthesize_findings
        result = repo.synthesize_findings("query", ["sq1"], ["f1", "f2"], "")
        assert "2 findings" in result
