"""Regression test: KNOWN_QUESTION_TYPES covers all standard Canvas types.

Previously `multiple_dropdowns_question` (and several other valid Canvas
question types) were absent from KNOWN_QUESTION_TYPES, so validating a staged
quiz that used them produced a spurious `unknown_question_type` error and
blocked the push. These tests pin the standard types as accepted and confirm a
genuinely bogus type is still rejected.
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))

import staging_schema  # noqa: E402


VALID_TYPES = [
    "multiple_choice_question",
    "multiple_answers_question",
    "true_false_question",
    "short_answer_question",
    "numerical_question",
    "fill_in_multiple_blanks_question",
    "multiple_dropdowns_question",
    "matching_question",
    "ordering_question",
    "calculated_question",
    "essay_question",
    "file_upload_question",
    "text_only_question",
]


def _codes(result):
    return {issue.code for issue in result.errors}


def test_standard_question_types_are_known():
    for qtype in VALID_TYPES:
        assert qtype in staging_schema.KNOWN_QUESTION_TYPES, qtype


def _validate_quiz_payload(payload):
    return staging_schema.validate_payload(
        payload, course_id="1", object_type="quizzes", object_id="1",
        meta={"module_number": 1, "title": "Q"},
    )


def test_multiple_dropdowns_does_not_error():
    payload = {
        "quiz": {"title": "Q"},
        "questions": [
            {
                "question_name": "Q1",
                "question_type": "multiple_dropdowns_question",
                "points_possible": 1,
                "answers": [
                    {"answer_text": "a", "answer_weight": 100, "blank_id": "b1"},
                    {"answer_text": "b", "answer_weight": 0, "blank_id": "b1"},
                ],
            }
        ],
    }
    result = _validate_quiz_payload(payload)
    assert "unknown_question_type" not in _codes(result)


def test_bogus_question_type_still_errors():
    payload = {
        "quiz": {"title": "Q"},
        "questions": [
            {
                "question_name": "Q1",
                "question_type": "not_a_real_question_type",
                "points_possible": 1,
                "answers": [{"answer_text": "a", "answer_weight": 100}],
            }
        ],
    }
    result = _validate_quiz_payload(payload)
    assert "unknown_question_type" in _codes(result)
