"""
Tests for the BaseQuestionGenerator class.

Tests cover:
- Initialization
- Format previous questions helper
"""

from unittest.mock import Mock


class ConcreteQuestionGenerator:
    """Concrete implementation for testing the base pattern."""

    def __init__(self, model):
        self.model = model

    def generate_questions(
        self,
        current_knowledge,
        query,
        questions_per_iteration,
        questions_by_iteration,
    ):
        return ["Question 1", "Question 2"]

    def _format_previous_questions(self, questions_by_iteration):
        formatted = []
        for iteration, questions in questions_by_iteration.items():
            formatted.append(f"Iteration {iteration}:")
            for q in questions:
                formatted.append(f"- {q}")
        return "\n".join(formatted)


class TestBaseQuestionGeneratorInit:
    """Tests for BaseQuestionGenerator initialization."""

    def test_init_stores_model(self):
        """Generator stores the model reference."""
        mock_model = Mock()
        generator = ConcreteQuestionGenerator(mock_model)
        assert generator.model is mock_model


class TestFormatPreviousQuestions:
    """Tests for _format_previous_questions helper."""

    def test_format_empty_questions(self):
        """Formats empty questions dict."""
        mock_model = Mock()
        generator = ConcreteQuestionGenerator(mock_model)

        result = generator._format_previous_questions({})

        assert result == ""

    def test_format_single_iteration(self):
        """Formats single iteration questions."""
        mock_model = Mock()
        generator = ConcreteQuestionGenerator(mock_model)

        questions = {1: ["What is X?", "How does Y work?"]}
        result = generator._format_previous_questions(questions)

        assert "Iteration 1:" in result
        assert "- What is X?" in result
        assert "- How does Y work?" in result

    def test_format_multiple_iterations(self):
        """Formats multiple iteration questions."""
        mock_model = Mock()
        generator = ConcreteQuestionGenerator(mock_model)

        questions = {
            1: ["Q1", "Q2"],
            2: ["Q3", "Q4"],
        }
        result = generator._format_previous_questions(questions)

        assert "Iteration 1:" in result
        assert "Iteration 2:" in result
        assert "- Q1" in result
        assert "- Q4" in result

    def test_format_preserves_order(self):
        """Formatted output preserves iteration order."""
        mock_model = Mock()
        generator = ConcreteQuestionGenerator(mock_model)

        questions = {1: ["First"], 2: ["Second"]}
        result = generator._format_previous_questions(questions)

        assert result.index("Iteration 1:") < result.index("Iteration 2:")


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

    def test_generate_questions_returns_list(self):
        """generate_questions returns a list."""
        mock_model = Mock()
        generator = ConcreteQuestionGenerator(mock_model)

        result = generator.generate_questions(
            current_knowledge="Some knowledge",
            query="Test query",
            questions_per_iteration=3,
            questions_by_iteration={},
        )

        assert isinstance(result, list)

    def test_generate_questions_returns_strings(self):
        """generate_questions returns list of strings."""
        mock_model = Mock()
        generator = ConcreteQuestionGenerator(mock_model)

        result = generator.generate_questions(
            current_knowledge="Knowledge",
            query="Query",
            questions_per_iteration=2,
            questions_by_iteration={},
        )

        for question in result:
            assert isinstance(question, str)
