"""
Tests for the NewsAnalyzer class.

Tests cover:
- Empty result handling
- News item extraction and validation
- Category counting
- Impact summarization
- Snippet preparation
"""

from unittest.mock import Mock


class TestNewsAnalyzer:
    """Tests for the NewsAnalyzer class."""

    def test_news_analyzer_empty_results(self):
        """Test with empty search results."""
        from local_deep_research.news.core.news_analyzer import NewsAnalyzer

        analyzer = NewsAnalyzer(llm_client=Mock())

        result = analyzer.analyze_news([])

        assert result["items"] == []
        assert result["item_count"] == 0
        assert result["big_picture"] == ""
        assert result["watch_for"] == []
        assert result["patterns"] == ""
        assert result["topics"] == []
        assert result["categories"] == {}
        assert result["impact_summary"]["average"] == 0
        assert result["impact_summary"]["high_impact_count"] == 0

    def test_validate_news_item(self):
        """Test field validation for news items."""
        from local_deep_research.news.core.news_analyzer import NewsAnalyzer

        analyzer = NewsAnalyzer(llm_client=Mock())

        # Valid item
        valid_item = {
            "headline": "Test headline",
            "summary": "Test summary",
            "category": "Tech",
        }
        assert analyzer._validate_news_item(valid_item) is True

        # Missing headline
        invalid_item1 = {"summary": "Test summary"}
        assert analyzer._validate_news_item(invalid_item1) is False

        # Missing summary
        invalid_item2 = {"headline": "Test headline"}
        assert analyzer._validate_news_item(invalid_item2) is False

        # Empty headline
        invalid_item3 = {"headline": "", "summary": "Test summary"}
        assert analyzer._validate_news_item(invalid_item3) is False

    def test_count_categories(self):
        """Test category grouping."""
        from local_deep_research.news.core.news_analyzer import NewsAnalyzer

        analyzer = NewsAnalyzer(llm_client=Mock())

        news_items = [
            {"category": "Technology", "headline": "Tech news 1"},
            {"category": "Technology", "headline": "Tech news 2"},
            {"category": "Sports", "headline": "Sports news"},
            {"category": "Politics", "headline": "Political news"},
            {"headline": "No category news"},  # Missing category
        ]

        result = analyzer._count_categories(news_items)

        assert result["Technology"] == 2
        assert result["Sports"] == 1
        assert result["Politics"] == 1
        assert result["Other"] == 1  # Default for missing category

    def test_summarize_impact(self):
        """Test impact statistics."""
        from local_deep_research.news.core.news_analyzer import NewsAnalyzer

        analyzer = NewsAnalyzer(llm_client=Mock())

        # Normal items with varying impact scores
        news_items = [
            {"impact_score": 9},
            {"impact_score": 8},
            {"impact_score": 5},
            {"impact_score": 3},
        ]

        result = analyzer._summarize_impact(news_items)

        assert result["average"] == 6.25
        assert result["high_impact_count"] == 2  # Scores >= 8
        assert result["max"] == 9
        assert result["min"] == 3

    def test_summarize_impact_empty(self):
        """Test impact statistics with empty list."""
        from local_deep_research.news.core.news_analyzer import NewsAnalyzer

        analyzer = NewsAnalyzer(llm_client=Mock())

        result = analyzer._summarize_impact([])

        assert result["average"] == 0
        assert result["high_impact_count"] == 0

    def test_prepare_snippets(self):
        """Test snippet formatting for LLM."""
        from local_deep_research.news.core.news_analyzer import NewsAnalyzer

        analyzer = NewsAnalyzer(llm_client=Mock())

        search_results = [
            {
                "title": "First Article",
                "url": "https://example.com/1",
                "snippet": "This is the first article snippet.",
            },
            {
                "title": "Second Article",
                "url": "https://example.com/2",
                "content": "This is the second article content.",
            },
        ]

        result = analyzer._prepare_snippets(search_results)

        assert "[1]" in result
        assert "[2]" in result
        assert "First Article" in result
        assert "https://example.com/1" in result
        assert "first article snippet" in result
        assert "Second Article" in result
        assert "second article content" in result

    def test_empty_analysis_structure(self):
        """Verify empty analysis structure has all required fields."""
        from local_deep_research.news.core.news_analyzer import NewsAnalyzer

        analyzer = NewsAnalyzer(llm_client=Mock())

        result = analyzer._empty_analysis()

        # Check all required fields are present
        required_fields = [
            "items",
            "item_count",
            "big_picture",
            "watch_for",
            "patterns",
            "topics",
            "categories",
            "impact_summary",
            "timestamp",
        ]

        for field in required_fields:
            assert field in result, f"Missing field: {field}"

        # Check types
        assert isinstance(result["items"], list)
        assert isinstance(result["item_count"], int)
        assert isinstance(result["big_picture"], str)
        assert isinstance(result["watch_for"], list)
        assert isinstance(result["patterns"], str)
        assert isinstance(result["topics"], list)
        assert isinstance(result["categories"], dict)
        assert isinstance(result["impact_summary"], dict)
        assert isinstance(result["timestamp"], str)
