import os
import sys

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

import audit_rubrics
import criterion_evaluator


def _criterion(name="Analysis", rating_count=4):
    return {
        "description": name,
        "ratings": [
            {"description": f"Level {idx}", "points": 4 - idx}
            for idx in range(rating_count)
        ],
    }


def test_audit_rubrics_flags_missing_rubric():
    findings = audit_rubrics.audit_rubrics(assignments=[
        {"id": 11, "name": "Artifact 1", "points_possible": 30}
    ])

    assert findings == [{
        "assignment_id": 11,
        "assignment_name": "Artifact 1",
        "issue_type": "missing_rubric",
        "detail": "Gradable assignment has no rubric attached.",
    }]


def test_audit_rubrics_flags_criteria_and_level_quality():
    findings = audit_rubrics.audit_rubrics(assignments=[
        {
            "id": 12,
            "name": "Artifact 2",
            "points_possible": 30,
            "rubric": [
                _criterion("10 points", rating_count=3),
                _criterion("Evidence", rating_count=4),
            ],
        }
    ])

    issue_types = {item["issue_type"] for item in findings}
    assert "rubric_criteria_count" in issue_types
    assert "rubric_performance_levels" in issue_types
    assert "point_count_description" in issue_types
    assert all(item["assignment_name"] == "Artifact 2" for item in findings)


def test_audit_rubrics_accepts_valid_canvas_rubric():
    findings = audit_rubrics.audit_rubrics(assignments=[
        {
            "id": 13,
            "name": "Artifact 3",
            "points_possible": 30,
            "rubric": [
                _criterion("Analysis"),
                _criterion("Evidence"),
                _criterion("Communication"),
                _criterion("Reflection"),
            ],
        }
    ])

    assert findings == []


def test_audit_rubrics_extracts_config_artifacts():
    course_config = {
        "modules": [
            {
                "number": 1,
                "assessments": {
                    "knowledge_check": {"title": "KC", "points": 10},
                    "artifact": {"title": "Artifact", "points": 30},
                },
            }
        ]
    }

    findings = audit_rubrics.audit_rubrics(course_config=course_config)

    assert len(findings) == 1
    assert findings[0]["assignment_name"] == "Artifact"
    assert findings[0]["issue_type"] == "missing_rubric"


def test_criterion_evaluator_collects_and_logs_rubric_findings(monkeypatch):
    monkeypatch.setattr(
        criterion_evaluator.audit_rubrics,
        "audit_rubrics",
        lambda **_kwargs: [{"issue_type": "missing_rubric", "detail": "No rubric"}],
    )
    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": [{"id": 1, "name": "Artifact", "points_possible": 30}],
    })
    result = criterion_evaluator._append_phase2_backlog_items("99992", findings)

    assert findings["rubric_findings"][0]["issue_type"] == "missing_rubric"
    assert result["count"] == 1
    assert added[0][1]["source"] == "audit_rubrics.py"
    assert added[0][1]["priority"] == "medium"
