"""Tests for the QuestionGenerator class."""

from unittest.mock import Mock, patch

import numpy as np
import pytest
from spacy.language import Language

from qz.question_generator import QuestionGenerator


@pytest.fixture(name="spacy_nlp")
def fixture_spacy_nlp():
    """Create a mock spacy NLP object."""
    nlp = Mock(spec=Language)
    doc_mock = Mock()
    doc_mock.sents = [
        Mock(text="Test sentence 1", root="test"),
        Mock(text="Test sentence 2", root="test"),
    ]
    nlp.return_value = doc_mock
    return nlp


@pytest.fixture(name="test_transformer")
def fixture_sentence_transformer():
    """Create a mock sentence transformer."""
    mock = Mock()
    mock.encode.return_value = np.array([[0.1, 0.2], [0.3, 0.4]])
    return mock


def test_initialization():
    """Test QuestionGenerator initialization."""
    with patch("spacy.load") as mock_load:
        mock_load.return_value = Mock(spec=Language)
        generator = QuestionGenerator()
        assert isinstance(generator, QuestionGenerator)
        assert generator.qa_model is not None
        assert generator.qa_tokenizer is not None
        assert generator.device is not None


def test_generate_question_from_text(spacy_nlp):
    """Test question generation from text."""
    with (
        patch("spacy.load", return_value=spacy_nlp),
        patch("transformers.AutoTokenizer.from_pretrained"),
        patch("transformers.AutoModelForQuestionAnswering.from_pretrained"),
        patch("transformers.BlipProcessor.from_pretrained"),
        patch("transformers.BlipForConditionalGeneration.from_pretrained"),
        patch("sentence_transformers.SentenceTransformer"),
    ):
        generator = QuestionGenerator()
        result = generator.generate_question_from_text("Test context")

        assert isinstance(result, dict)
        assert "text" in result
        assert "context" in result
        assert "answer" in result
        assert result["context"] == "Test context"


def test_generate_distractors(spacy_nlp, test_transformer):
    """Test distractor generation."""
    generator = QuestionGenerator(nlp=spacy_nlp, sentence_model=test_transformer)
    distractors = generator.generate_distractors(
        correct_answer="test answer", context="Test context with some content", num_distractors=3
    )
    assert isinstance(distractors, list)
    assert len(distractors) == 3


def test_create_quiz_question():
    """Test complete quiz question creation."""
    with (
        patch.object(QuestionGenerator, "generate_question_from_text") as mock_gen_question,
        patch.object(QuestionGenerator, "generate_distractors") as mock_gen_distractors,
    ):
        mock_gen_question.return_value = {
            "text": "Test question?",
            "context": "Test context",
            "answer": "correct answer",
        }
        mock_gen_distractors.return_value = ["wrong1", "wrong2", "wrong3"]

        generator = QuestionGenerator()
        result = generator.create_quiz_question("Test context")

        assert isinstance(result, dict)
        assert "text" in result
        assert "options" in result
        assert "correct_answer" in result
        assert len(result["options"]) == 4
        assert result["options"][result["correct_answer"]] == "correct answer"
        assert "difficulty" in result
        assert "category" in result
