"""
Tests for NewsQuestionGenerator.generate_questions() pure logic.

Existing tests: none — this file is the first test coverage.

Covers:
- Default query generation (base_queries list slicing)
- Focused query generation when user provides specific query
- Date formatting in queries
- questions_per_iteration limiting
- Edge cases: empty query, "latest important news today" passthrough
"""

from unittest.mock import Mock

from freezegun import freeze_time


def _make_generator():
    """Create a NewsQuestionGenerator with a mocked LLM."""
    from local_deep_research.advanced_search_system.questions.news_question import (
        NewsQuestionGenerator,
    )

    return NewsQuestionGenerator(model=Mock())


class TestDefaultQueryGeneration:
    """Tests for base query generation without specific focus."""

    @freeze_time("2025-03-15")
    def test_returns_list_of_strings(self):
        """Returns a list of string queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        assert isinstance(result, list)
        assert all(isinstance(q, str) for q in result)

    @freeze_time("2025-03-15")
    def test_default_returns_8_queries(self):
        """Default questions_per_iteration=8 returns 8 queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        assert len(result) == 8

    @freeze_time("2025-03-15")
    def test_respects_questions_per_iteration(self):
        """Returns only the requested number of queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
            questions_per_iteration=3,
        )
        assert len(result) == 3

    @freeze_time("2025-03-15")
    def test_contains_date_string(self):
        """Base queries contain today's formatted date."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        date_queries = [q for q in result if "March 15, 2025" in q]
        assert len(date_queries) > 0

    @freeze_time("2025-03-15")
    def test_breaking_news_in_queries(self):
        """At least one query contains 'breaking news'."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        assert any("breaking news" in q for q in result)

    @freeze_time("2025-03-15")
    def test_covers_diverse_categories(self):
        """Queries cover diverse news categories."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        all_text = " ".join(result)
        assert "economic" in all_text.lower()
        assert "political" in all_text.lower()
        assert "technology" in all_text.lower()

    @freeze_time("2025-12-25")
    def test_date_format_december(self):
        """Date formatting works for different months."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        date_queries = [q for q in result if "December 25, 2025" in q]
        assert len(date_queries) > 0


class TestFocusedQueryGeneration:
    """Tests when user provides a specific query focus."""

    @freeze_time("2025-06-01")
    def test_focused_returns_8_queries(self):
        """Focused query returns 3 focus + 5 base = 8 queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="climate change",
        )
        assert len(result) == 8

    @freeze_time("2025-06-01")
    def test_focus_queries_come_first(self):
        """Focus-specific queries appear before base queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="AI regulation",
        )
        # First 3 should contain the user's query
        assert "AI regulation" in result[0]
        assert "AI regulation" in result[1]
        assert "AI regulation" in result[2]

    @freeze_time("2025-06-01")
    def test_focus_query_includes_date(self):
        """First focus query includes the date."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="elections",
        )
        assert "June 01, 2025" in result[0]

    @freeze_time("2025-06-01")
    def test_focus_query_includes_breaking_news(self):
        """Second focus query includes 'breaking news'."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="elections",
        )
        assert "breaking news" in result[1]

    @freeze_time("2025-06-01")
    def test_focus_query_includes_latest_developments(self):
        """Third focus query includes 'latest developments'."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="elections",
        )
        assert "latest developments" in result[2]

    @freeze_time("2025-06-01")
    def test_base_queries_follow_focus(self):
        """Base queries follow the 3 focus queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="space exploration",
        )
        # Items 3-7 should be from the base queries (first 5)
        assert len(result) == 8
        # These should NOT contain the user query
        base_part = result[3:]
        assert all("space exploration" not in q for q in base_part)


class TestEmptyAndEdgeCaseQueries:
    """Tests for edge case query values."""

    @freeze_time("2025-06-01")
    def test_empty_string_query_uses_base(self):
        """Empty string query returns base queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="",
        )
        # Empty query is falsy, so base queries are used
        assert len(result) == 8

    @freeze_time("2025-06-01")
    def test_exact_default_query_uses_base(self):
        """The exact string 'latest important news today' uses base queries."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        # This specific string triggers the default path
        assert len(result) == 8
        # None should contain "latest important news today" as a prefix
        # (they're base queries, not focus queries)
        assert "latest important news today" not in result[0]

    @freeze_time("2025-06-01")
    def test_questions_per_iteration_larger_than_base(self):
        """Requesting more questions than base queries returns all available."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
            questions_per_iteration=20,
        )
        # Only 8 base queries exist
        assert len(result) == 8

    @freeze_time("2025-06-01")
    def test_questions_per_iteration_zero(self):
        """Zero questions_per_iteration returns empty list."""
        gen = _make_generator()
        result = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
            questions_per_iteration=0,
        )
        assert result == []

    @freeze_time("2025-06-01")
    def test_none_questions_by_iteration(self):
        """None questions_by_iteration is accepted."""
        gen = _make_generator()
        # Should not raise
        result = gen.generate_questions(
            current_knowledge="some knowledge",
            query="latest important news today",
            questions_by_iteration=None,
        )
        assert isinstance(result, list)

    @freeze_time("2025-06-01")
    def test_current_knowledge_ignored(self):
        """current_knowledge doesn't affect output (news uses template queries)."""
        gen = _make_generator()
        result_empty = gen.generate_questions(
            current_knowledge="",
            query="latest important news today",
        )
        result_full = gen.generate_questions(
            current_knowledge="Lots of existing knowledge about many topics.",
            query="latest important news today",
        )
        assert result_empty == result_full


class TestBaseQuestionGeneratorFormatting:
    """Tests for _format_previous_questions from BaseQuestionGenerator."""

    def test_format_previous_questions_empty(self):
        """Empty dict returns empty string."""
        gen = _make_generator()
        result = gen._format_previous_questions({})
        assert result == ""

    def test_format_previous_questions_single_iteration(self):
        """Single iteration formatted correctly."""
        gen = _make_generator()
        result = gen._format_previous_questions({1: ["Q1", "Q2"]})
        assert "Iteration 1:" in result
        assert "- Q1" in result
        assert "- Q2" in result

    def test_format_previous_questions_multiple_iterations(self):
        """Multiple iterations formatted correctly."""
        gen = _make_generator()
        result = gen._format_previous_questions({1: ["Q1"], 2: ["Q2", "Q3"]})
        assert "Iteration 1:" in result
        assert "Iteration 2:" in result
        assert "- Q1" in result
        assert "- Q2" in result
        assert "- Q3" in result
