"""
Tests for advanced_search_system/questions/followup/simple_followup_question.py

Tests cover:
- SimpleFollowUpQuestionGenerator initialization
- generate_contextualized_query method
- Context formatting
"""

from unittest.mock import Mock


class TestSimpleFollowUpQuestionGeneratorInit:
    """Tests for SimpleFollowUpQuestionGenerator initialization."""

    def test_inherits_from_base_followup(self):
        """Test that SimpleFollowUpQuestionGenerator inherits from BaseFollowUpQuestionGenerator."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )
        from local_deep_research.advanced_search_system.questions.followup.base_followup_question import (
            BaseFollowUpQuestionGenerator,
        )

        assert issubclass(
            SimpleFollowUpQuestionGenerator, BaseFollowUpQuestionGenerator
        )

    def test_init_with_model(self):
        """Test initialization with a model."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        mock_model = Mock()
        generator = SimpleFollowUpQuestionGenerator(mock_model)

        assert generator.model is mock_model


class TestGenerateContextualizedQuery:
    """Tests for generate_contextualized_query method."""

    def test_includes_follow_up_query(self):
        """Test that follow-up query is included."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())

        result = generator.generate_contextualized_query(
            follow_up_query="What about the details?",
            original_query="Original topic",
            past_findings="Some findings",
        )

        assert "What about the details?" in result

    def test_includes_original_query(self):
        """Test that original query is included."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())

        result = generator.generate_contextualized_query(
            follow_up_query="Follow up",
            original_query="What is quantum computing?",
            past_findings="Findings",
        )

        assert "What is quantum computing?" in result

    def test_includes_past_findings(self):
        """Test that past findings are included."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())
        past_findings = "Quantum computing uses qubits for parallel processing."

        result = generator.generate_contextualized_query(
            follow_up_query="Tell me more",
            original_query="Quantum computing",
            past_findings=past_findings,
        )

        assert past_findings in result

    def test_includes_important_header(self):
        """Test that IMPORTANT header is included."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())

        result = generator.generate_contextualized_query(
            follow_up_query="Follow up",
            original_query="Original",
            past_findings="Findings",
        )

        assert "IMPORTANT" in result
        assert "follow-up request" in result.lower()

    def test_includes_user_request_label(self):
        """Test that USER'S FOLLOW-UP REQUEST label is included."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())

        result = generator.generate_contextualized_query(
            follow_up_query="What about X?",
            original_query="Topic Y",
            past_findings="Previous findings",
        )

        assert "USER'S FOLLOW-UP REQUEST" in result

    def test_structure_has_sections(self):
        """Test that the output has proper section structure."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())

        result = generator.generate_contextualized_query(
            follow_up_query="Follow up question",
            original_query="Original question",
            past_findings="These are the previous findings",
        )

        assert "Previous research query:" in result
        assert "Previous findings:" in result
        assert "---" in result

    def test_preserves_exact_queries(self):
        """Test that exact queries are preserved without modification."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())
        follow_up = "provide data in a table format"
        original = "climate change impacts"
        findings = "Temperature rise by 1.5C expected"

        result = generator.generate_contextualized_query(
            follow_up_query=follow_up,
            original_query=original,
            past_findings=findings,
        )

        assert follow_up in result
        assert original in result
        assert findings in result

    def test_handles_empty_past_findings(self):
        """Test handling of empty past findings."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())

        result = generator.generate_contextualized_query(
            follow_up_query="Follow up",
            original_query="Original",
            past_findings="",
        )

        # Should still work with empty findings
        assert "Previous findings:" in result
        assert "Follow up" in result

    def test_handles_multiline_findings(self):
        """Test handling of multiline past findings."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())
        multiline_findings = """Finding 1: Important fact
Finding 2: Another fact
Finding 3: More information"""

        result = generator.generate_contextualized_query(
            follow_up_query="Summarize findings",
            original_query="Research topic",
            past_findings=multiline_findings,
        )

        assert "Finding 1" in result
        assert "Finding 2" in result
        assert "Finding 3" in result

    def test_ignores_kwargs(self):
        """Test that extra kwargs are ignored."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )

        generator = SimpleFollowUpQuestionGenerator(Mock())

        # Should not raise an error
        result = generator.generate_contextualized_query(
            follow_up_query="Query",
            original_query="Original",
            past_findings="Findings",
            extra_param="ignored",
            another_param=123,
        )

        assert "Query" in result


class TestLogOutput:
    """Tests for logging behavior."""

    def test_logs_context_length(self):
        """Test that context length is logged."""
        from local_deep_research.advanced_search_system.questions.followup.simple_followup_question import (
            SimpleFollowUpQuestionGenerator,
        )
        from unittest.mock import patch

        generator = SimpleFollowUpQuestionGenerator(Mock())
        findings = "X" * 1000

        with patch(
            "local_deep_research.advanced_search_system.questions.followup.simple_followup_question.logger"
        ) as mock_logger:
            generator.generate_contextualized_query(
                follow_up_query="Query",
                original_query="Original",
                past_findings=findings,
            )

            mock_logger.info.assert_called_once()
            call_args = str(mock_logger.info.call_args)
            assert "1000" in call_args
