"""
Tests for advanced_search_system/filters/base_filter.py

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

from unittest.mock import Mock

import pytest


class TestBaseFilterInit:
    """Tests for BaseFilter initialization."""

    def test_init_with_model(self):
        """Test initialization with model."""
        from local_deep_research.advanced_search_system.filters.base_filter import (
            BaseFilter,
        )

        # Create a concrete implementation for testing
        class ConcreteFilter(BaseFilter):
            def filter_results(self, results, query, **kwargs):
                return results

        mock_model = Mock()
        filter_instance = ConcreteFilter(model=mock_model)

        assert filter_instance.model is mock_model

    def test_init_without_model(self):
        """Test initialization without model."""
        from local_deep_research.advanced_search_system.filters.base_filter import (
            BaseFilter,
        )

        class ConcreteFilter(BaseFilter):
            def filter_results(self, results, query, **kwargs):
                return results

        filter_instance = ConcreteFilter()

        assert filter_instance.model is None


class TestBaseFilterAbstract:
    """Tests for BaseFilter abstract method."""

    def test_cannot_instantiate_directly(self):
        """Test that BaseFilter cannot be instantiated directly."""
        from local_deep_research.advanced_search_system.filters.base_filter import (
            BaseFilter,
        )

        with pytest.raises(TypeError):
            BaseFilter()

    def test_subclass_must_implement_filter_results(self):
        """Test that subclass must implement filter_results."""
        from local_deep_research.advanced_search_system.filters.base_filter import (
            BaseFilter,
        )

        class IncompleteFilter(BaseFilter):
            pass

        with pytest.raises(TypeError):
            IncompleteFilter()

    def test_concrete_implementation_works(self):
        """Test that concrete implementation can be instantiated."""
        from local_deep_research.advanced_search_system.filters.base_filter import (
            BaseFilter,
        )

        class ConcreteFilter(BaseFilter):
            def filter_results(self, results, query, **kwargs):
                return [
                    r
                    for r in results
                    if query.lower() in r.get("title", "").lower()
                ]

        filter_instance = ConcreteFilter()
        results = [{"title": "Python Tutorial"}, {"title": "Java Guide"}]
        filtered = filter_instance.filter_results(results, "python")

        assert len(filtered) == 1
        assert filtered[0]["title"] == "Python Tutorial"


class TestBaseFilterMethodSignature:
    """Tests for filter_results method signature."""

    def test_filter_results_accepts_kwargs(self):
        """Test that filter_results accepts kwargs."""
        from local_deep_research.advanced_search_system.filters.base_filter import (
            BaseFilter,
        )

        class ConcreteFilter(BaseFilter):
            def filter_results(self, results, query, **kwargs):
                custom_param = kwargs.get("custom_param", False)
                if custom_param:
                    return results[:1]
                return results

        filter_instance = ConcreteFilter()
        results = [{"title": "A"}, {"title": "B"}]

        # Without custom param
        assert len(filter_instance.filter_results(results, "query")) == 2

        # With custom param
        assert (
            len(
                filter_instance.filter_results(
                    results, "query", custom_param=True
                )
            )
            == 1
        )
