import os
import sys

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

import audit_clo_orphans
import criterion_evaluator


def test_clo_orphans_flags_clo_without_mlos():
    config = {
        "clos": [{"id": "CLO-1", "text": "Analyze cases."}],
        "modules": [{
            "number": 1,
            "objectives": [{"id": "M1.1", "text": "Analyze cases.", "clo": "CLO-2"}],
            "assessments": {"artifact": {"title": "Artifact", "points": 30}},
        }],
    }

    findings = audit_clo_orphans.audit_clo_orphans(course_config=config)

    assert findings == [{
        "clo_id": "CLO-1",
        "clo_text": "Analyze cases.",
        "issue_type": "orphan_clo",
        "detail": "CLO has no mapped module learning objectives.",
    }]


def test_clo_orphans_flags_hollow_clo():
    config = {
        "clos": [{"id": "CLO-1", "text": "Evaluate cases."}],
        "modules": [{
            "number": 1,
            "title": "Case Work",
            "objectives": [{"id": "M1.1", "text": "Evaluate cases.", "clo": "CLO-1"}],
            "assessments": {},
        }],
    }

    findings = audit_clo_orphans.audit_clo_orphans(course_config=config)

    assert len(findings) == 1
    assert findings[0]["issue_type"] == "hollow_clo"
    assert "Case Work" in findings[0]["detail"]


def test_clo_orphans_allows_assessed_mapped_clo():
    config = {
        "clos": [{"id": "CLO-1", "text": "Evaluate cases."}],
        "modules": [{
            "number": 1,
            "objectives": [{"id": "M1.1", "text": "Evaluate cases.", "clo": ["CLO-1"]}],
            "assessments": {"knowledge_check": {"title": "KC", "points": 10}},
        }],
    }

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


def test_criterion_evaluator_collects_all_phase2_keys_and_logs_clo(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: [])
    monkeypatch.setattr(
        criterion_evaluator.audit_clo_orphans,
        "audit_clo_orphans",
        lambda **_kwargs: [{"issue_type": "orphan_clo", "detail": "No MLO coverage"}],
    )
    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 set(findings) == {
        "rubric_findings",
        "mlo_drift_findings",
        "bloom_findings",
        "clo_orphan_findings",
        "mlo_overclaim_findings",
        "image_alt_findings",
    }
    assert result["count"] == 1
    assert added[0][1]["source"] == "audit_clo_orphans.py"
    assert added[0][1]["priority"] == "medium"
