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

Tests cover:
- LLMFollowUpQuestionGenerator initialization
- generate_contextualized_query method (fallback behavior)
- generate_questions method (fallback behavior)
"""

from unittest.mock import Mock, patch


class TestLLMFollowUpQuestionGeneratorInit:
    """Tests for LLMFollowUpQuestionGenerator initialization."""

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

        assert issubclass(
            LLMFollowUpQuestionGenerator, BaseFollowUpQuestionGenerator
        )

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

        mock_model = Mock()
        generator = LLMFollowUpQuestionGenerator(mock_model)

        assert generator.model is mock_model


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

    def test_falls_back_to_simple_generator(self):
        """Test that it falls back to SimpleFollowUpQuestionGenerator."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        generator = LLMFollowUpQuestionGenerator(Mock())

        result = generator.generate_contextualized_query(
            follow_up_query="Follow up question",
            original_query="Original query",
            past_findings="Past findings",
        )

        # Should produce SimpleFollowUpQuestionGenerator style output
        assert "IMPORTANT" in result
        assert "Follow up question" in result
        assert "Original query" in result
        assert "Past findings" in result

    def test_logs_warning_about_not_implemented(self):
        """Test that a warning is logged about not being implemented."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        generator = LLMFollowUpQuestionGenerator(Mock())

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

            mock_logger.warning.assert_called_once()
            call_args = str(mock_logger.warning.call_args)
            assert "not yet implemented" in call_args.lower()

    def test_passes_all_parameters_to_simple_generator(self):
        """Test that all parameters are passed to SimpleFollowUpQuestionGenerator."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        generator = LLMFollowUpQuestionGenerator(Mock())

        # Test by verifying the output matches SimpleFollowUpQuestionGenerator behavior
        result = generator.generate_contextualized_query(
            follow_up_query="follow up",
            original_query="original",
            past_findings="findings",
            extra_kwarg="extra",
        )

        # Should contain all the parameters in the output
        assert "follow up" in result
        assert "original" in result
        assert "findings" in result

    def test_uses_same_model_for_simple_generator(self):
        """Test that the model is properly stored and accessible."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        mock_model = Mock()
        generator = LLMFollowUpQuestionGenerator(mock_model)

        # Verify the model is stored
        assert generator.model is mock_model

        # Verify it still works (fallback to simple generator works)
        result = generator.generate_contextualized_query(
            follow_up_query="query",
            original_query="original",
            past_findings="findings",
        )

        # Should produce valid output
        assert "query" in result
        assert "original" in result


class TestGenerateQuestions:
    """Tests for generate_questions method."""

    def test_returns_single_question_list(self):
        """Test that generate_questions returns single item list."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        generator = LLMFollowUpQuestionGenerator(Mock())

        result = generator.generate_questions(
            current_knowledge="knowledge",
            query="test query",
            questions_per_iteration=5,
            questions_by_iteration={},
        )

        assert result == ["test query"]

    def test_calls_parent_generate_questions(self):
        """Test that parent's generate_questions is called."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        generator = LLMFollowUpQuestionGenerator(Mock())

        # Call with specific parameters
        result = generator.generate_questions(
            current_knowledge="accumulated knowledge",
            query="the query",
            questions_per_iteration=10,
            questions_by_iteration={1: ["prev question"]},
        )

        # Should return the query as a single item list (from parent behavior)
        assert len(result) == 1
        assert result[0] == "the query"

    def test_ignores_questions_per_iteration(self):
        """Test that questions_per_iteration is ignored."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        generator = LLMFollowUpQuestionGenerator(Mock())

        result1 = generator.generate_questions(
            current_knowledge="",
            query="query",
            questions_per_iteration=1,
            questions_by_iteration={},
        )

        result10 = generator.generate_questions(
            current_knowledge="",
            query="query",
            questions_per_iteration=10,
            questions_by_iteration={},
        )

        # Both should return single item
        assert len(result1) == len(result10) == 1

    def test_ignores_questions_by_iteration(self):
        """Test that questions_by_iteration is ignored."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        generator = LLMFollowUpQuestionGenerator(Mock())

        result = generator.generate_questions(
            current_knowledge="",
            query="query",
            questions_per_iteration=5,
            questions_by_iteration={
                1: ["q1", "q2"],
                2: ["q3", "q4"],
            },
        )

        # Should still return single item regardless of previous questions
        assert result == ["query"]


class TestFutureImplementation:
    """Tests documenting expected future behavior."""

    def test_docstring_mentions_future_features(self):
        """Test that docstring mentions planned features."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        docstring = (
            LLMFollowUpQuestionGenerator.generate_contextualized_query.__doc__
        )

        assert "Future" in docstring or "future" in docstring.lower()

    def test_class_docstring_notes_placeholder(self):
        """Test that class docstring notes it's a placeholder."""
        from local_deep_research.advanced_search_system.questions.followup.llm_followup_question import (
            LLMFollowUpQuestionGenerator,
        )

        docstring = LLMFollowUpQuestionGenerator.__doc__

        assert "placeholder" in docstring.lower() or "NOTE" in docstring
