import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "scripts"))

import audit_bloom_alignment
import criterion_evaluator


def test_bloom_alignment_flags_quiz_above_mlo_ceiling():
    config = {
        "modules": [{
            "number": 1,
            "objectives": [{"id": "M1.1", "text": "Describe system models."}],
            "assessments": {
                "knowledge_check": {
                    "questions": [{"question_text": "Evaluate system models."}]
                },
                "artifact": {"title": "Artifact", "prompt": "Describe system models."},
            },
        }],
    }

    findings = audit_bloom_alignment.audit_bloom_alignment(course_config=config)

    assert findings[0]["issue"] == "quiz_level_above_mlo_ceiling"
    assert findings[0]["item_type"] == "knowledge_check"
    assert findings[0]["mlo_bloom_level"] == "Understand"
    assert findings[0]["item_bloom_level"] == "Evaluate"


def test_bloom_alignment_flags_artifact_below_mlo_floor():
    config = {
        "modules": [{
            "number": 2,
            "objectives": [{"id": "M2.1", "text": "Analyze case data."}],
            "assessments": {
                "artifact": {"title": "Artifact", "prompt": "Identify case data."},
            },
        }],
    }

    findings = audit_bloom_alignment.audit_bloom_alignment(course_config=config)

    assert any(item["issue"] == "assignment_level_below_mlo_floor" for item in findings)
    assert any(item["issue"] == "missing_upper_order_assessment" for item in findings)


def test_bloom_alignment_allows_upper_order_artifact():
    config = {
        "modules": [{
            "number": 3,
            "objectives": [{"id": "M3.1", "text": "Analyze case data."}],
            "assessments": {
                "knowledge_check": {"questions": [{"question_text": "Identify case data."}]},
                "artifact": {"title": "Artifact", "prompt": "Analyze case data."},
            },
        }],
    }

    assert audit_bloom_alignment.audit_bloom_alignment(course_config=config) == []


def test_criterion_evaluator_collects_bloom_findings(monkeypatch):
    monkeypatch.setattr(criterion_evaluator.audit_rubrics, "audit_rubrics", lambda **_kwargs: [])
    monkeypatch.setattr(criterion_evaluator.audit_mlo_drift, "audit_mlo_drift", lambda **_kwargs: [])
    monkeypatch.setattr(
        criterion_evaluator.audit_bloom_alignment,
        "audit_bloom_alignment",
        lambda **_kwargs: [{"issue_type": "missing_upper_order_assessment", "detail": "Missing"}],
    )
    added = []
    monkeypatch.setattr(
        criterion_evaluator.backlog_manager,
        "add",
        lambda course_id, item: added.append((course_id, item)) or item,
    )

    findings = criterion_evaluator._collect_phase2_findings({"course_id": "99992", "assignments": []})
    result = criterion_evaluator._append_phase2_backlog_items("99992", findings)

    assert findings["bloom_findings"][0]["issue_type"] == "missing_upper_order_assessment"
    assert result["count"] == 1
    assert added[0][1]["source"] == "audit_bloom_alignment.py"
