"""
Deep behavioral tests for TopicBasedRecommender and SearchBasedRecommender.
Tests topic filtering, preference matching, query generation, and trending fallback.
"""

from unittest.mock import Mock

from local_deep_research.news.recommender.topic_based import (
    TopicBasedRecommender,
    SearchBasedRecommender,
)


def _make_recommender(**kwargs):
    """Create a TopicBasedRecommender with optional mocks."""
    return TopicBasedRecommender(**kwargs)


# --- Init ---


class TestTopicBasedRecommenderInit:
    """Tests for TopicBasedRecommender initialization."""

    def test_default_max_recommendations(self):
        r = _make_recommender()
        assert r.max_recommendations == 5

    def test_inherits_base_deps(self):
        pm = Mock()
        r = _make_recommender(preference_manager=pm)
        assert r.preference_manager is pm

    def test_strategy_name(self):
        r = _make_recommender()
        assert r.strategy_name == "TopicBasedRecommender"


# --- _filter_topics_by_preferences ---


class TestFilterTopicsByPreferences:
    """Tests for preference-based topic filtering."""

    def test_empty_topics(self):
        r = _make_recommender()
        result = r._filter_topics_by_preferences([], {})
        assert result == []

    def test_empty_preferences(self):
        r = _make_recommender()
        topics = ["AI", "Climate", "Sports"]
        result = r._filter_topics_by_preferences(topics, {})
        assert result == ["AI", "Climate", "Sports"]

    def test_filters_disliked_topics(self):
        r = _make_recommender()
        topics = ["AI news", "Sports update", "Climate change"]
        prefs = {"disliked_topics": ["sports"]}
        result = r._filter_topics_by_preferences(topics, prefs)
        assert "Sports update" not in result
        assert "AI news" in result
        assert "Climate change" in result

    def test_disliked_topics_case_insensitive(self):
        r = _make_recommender()
        topics = ["SPORTS Update", "AI News"]
        prefs = {"disliked_topics": ["sports"]}
        result = r._filter_topics_by_preferences(topics, prefs)
        assert "SPORTS Update" not in result

    def test_disliked_partial_match(self):
        """Disliked 'sport' should match 'Sports update'."""
        r = _make_recommender()
        topics = ["Sports update", "Tech news"]
        prefs = {"disliked_topics": ["sport"]}
        result = r._filter_topics_by_preferences(topics, prefs)
        assert "Sports update" not in result

    def test_interests_boost_ordering(self):
        r = _make_recommender()
        topics = ["Climate change", "AI developments", "Economic policy"]
        prefs = {"interests": {"ai": 5.0, "climate": 2.0}}
        result = r._filter_topics_by_preferences(topics, prefs)
        # AI should be first (weight 5.0), then Climate (2.0), then Economic (1.0)
        assert result[0] == "AI developments"
        assert result[1] == "Climate change"
        assert result[2] == "Economic policy"

    def test_interests_case_insensitive(self):
        r = _make_recommender()
        topics = ["AI Research"]
        prefs = {"interests": {"ai": 3.0}}
        result = r._filter_topics_by_preferences(topics, prefs)
        assert result == ["AI Research"]

    def test_unmatched_topics_have_default_boost(self):
        r = _make_recommender()
        topics = ["Unknown Topic", "AI News"]
        prefs = {"interests": {"ai": 2.0}}
        result = r._filter_topics_by_preferences(topics, prefs)
        # AI News boosted to 2.0, Unknown stays at 1.0
        assert result[0] == "AI News"
        assert result[1] == "Unknown Topic"

    def test_combined_filter_and_boost(self):
        r = _make_recommender()
        topics = ["AI News", "Sports Score", "Climate Report"]
        prefs = {
            "disliked_topics": ["sports"],
            "interests": {"climate": 3.0, "ai": 1.5},
        }
        result = r._filter_topics_by_preferences(topics, prefs)
        assert "Sports Score" not in result
        assert result[0] == "Climate Report"  # Weight 3.0
        assert result[1] == "AI News"  # Weight 1.5

    def test_multiple_disliked_topics(self):
        r = _make_recommender()
        topics = ["AI", "Sports", "Politics", "Tech"]
        prefs = {"disliked_topics": ["sports", "politics"]}
        result = r._filter_topics_by_preferences(topics, prefs)
        assert result == ["AI", "Tech"]

    def test_all_topics_disliked(self):
        r = _make_recommender()
        topics = ["Sports news", "Football scores"]
        prefs = {"disliked_topics": ["sports", "football"]}
        result = r._filter_topics_by_preferences(topics, prefs)
        assert result == []

    def test_preserves_order_without_interests(self):
        r = _make_recommender()
        topics = ["A", "B", "C"]
        prefs = {}
        result = r._filter_topics_by_preferences(topics, prefs)
        # All have boost 1.0, sort is stable
        assert len(result) == 3

    def test_first_interest_match_wins(self):
        """Only the first matching interest's weight is used."""
        r = _make_recommender()
        topics = ["AI Machine Learning"]
        prefs = {"interests": {"ai": 2.0, "machine": 5.0}}
        result = r._filter_topics_by_preferences(topics, prefs)
        # Should match first interest ("ai") and get weight 2.0
        assert result == ["AI Machine Learning"]


# --- _generate_topic_query ---


class TestGenerateTopicQuery:
    """Tests for topic query generation."""

    def test_includes_topic(self):
        r = _make_recommender()
        query = r._generate_topic_query("artificial intelligence")
        assert "artificial intelligence" in query

    def test_includes_news_context(self):
        r = _make_recommender()
        query = r._generate_topic_query("AI")
        assert "news" in query.lower()
        assert "latest" in query.lower()

    def test_includes_breaking(self):
        r = _make_recommender()
        query = r._generate_topic_query("AI")
        assert "breaking" in query.lower()


# --- _get_trending_topics ---


class TestGetTrendingTopics:
    """Tests for trending topic retrieval."""

    def test_returns_defaults_when_no_registry(self):
        r = _make_recommender()
        topics = r._get_trending_topics(None)
        assert len(topics) > 0
        assert any("artificial intelligence" in t for t in topics)

    def test_uses_topic_registry(self):
        registry = Mock()
        registry.get_trending_topics.return_value = ["Topic A", "Topic B"]
        r = _make_recommender(topic_registry=registry)
        topics = r._get_trending_topics(None)
        assert "Topic A" in topics
        assert "Topic B" in topics

    def test_adds_context_topics(self):
        r = _make_recommender()
        context = {"current_news_topics": ["Breaking Event"]}
        topics = r._get_trending_topics(context)
        assert "Breaking Event" in topics

    def test_empty_registry_falls_back_to_defaults(self):
        registry = Mock()
        registry.get_trending_topics.return_value = []
        r = _make_recommender(topic_registry=registry)
        topics = r._get_trending_topics(None)
        # Should get defaults since registry returned empty
        assert len(topics) > 0

    def test_combines_registry_and_context(self):
        registry = Mock()
        registry.get_trending_topics.return_value = ["Registry Topic"]
        r = _make_recommender(topic_registry=registry)
        context = {"current_news_topics": ["Context Topic"]}
        topics = r._get_trending_topics(context)
        assert "Registry Topic" in topics
        assert "Context Topic" in topics


# --- SearchBasedRecommender ---


class TestSearchBasedRecommender:
    """Tests for SearchBasedRecommender."""

    def test_returns_empty_list(self):
        r = SearchBasedRecommender()
        result = r.generate_recommendations("user1")
        assert result == []

    def test_strategy_name(self):
        r = SearchBasedRecommender()
        assert r.strategy_name == "SearchBasedRecommender"

    def test_with_context_still_empty(self):
        r = SearchBasedRecommender()
        result = r.generate_recommendations("user1", context={"key": "val"})
        assert result == []
