import os
import sys

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

import backlog_manager

COURSE_ID = "99995"


def _use_tmp_plugin_root(monkeypatch, tmp_path):
    monkeypatch.setattr(backlog_manager, "get_plugin_root", lambda: str(tmp_path))
    (tmp_path / "courses" / COURSE_ID).mkdir(parents=True, exist_ok=True)


def test_file_a11y_template_renders_fix_path():
    fix_path = backlog_manager.build_fix_path(
        {
            "finding_type": "file_a11y",
            "file_name": "lecture-notes.pdf",
            "issue": "class=scanned, route=OCR+E2",
        },
        course_id="260866",
    )

    assert "file-remediation" in fix_path
    assert "lecture-notes.pdf" in fix_path
    assert "260866" in fix_path
    assert "class=scanned, route=OCR+E2" in fix_path


def test_file_a11y_backlog_entry_has_file_remediation_fix_path(monkeypatch, tmp_path):
    _use_tmp_plugin_root(monkeypatch, tmp_path)

    record = backlog_manager.add(COURSE_ID, {
        "finding_type": "file_a11y",
        "detail": "Untagged PDF routed to E2-insitu.",
        "file_name": "syllabus.pdf",
        "issue": "class=digital-untagged, route=E2-insitu",
    })

    assert "file-remediation" in record["fix_path"]
    assert "syllabus.pdf" in record["fix_path"]


def test_file_a11y_is_b_allowlisted():
    assert "file_a11y" in backlog_manager.B_FIX_PATH_TYPES


def test_resolve_file_a11y_matches_by_file_id(monkeypatch, tmp_path):
    _use_tmp_plugin_root(monkeypatch, tmp_path)

    record = backlog_manager.add(COURSE_ID, {
        "finding_type": "file_a11y",
        "detail": "Untagged PDF routed to E2-insitu.",
        "file_name": "12345-syllabus.pdf",  # remediate_assess.py's "{id}-{name}" shape
        "file_id": 12345,
        "issue": "class=digital-untagged, route=E2-insitu",
    })

    resolved = backlog_manager.resolve_file_a11y(COURSE_ID, file_id=12345)

    assert resolved == [record["id"]]
    items = backlog_manager.list(COURSE_ID)
    assert items[0]["resolved"] is True
    assert items[0]["resolved_at"]


def test_resolve_file_a11y_falls_back_to_file_name_suffix(monkeypatch, tmp_path):
    _use_tmp_plugin_root(monkeypatch, tmp_path)

    record = backlog_manager.add(COURSE_ID, {
        "finding_type": "file_a11y",
        "detail": "Missing alt text.",
        "file_name": "999-lecture-notes.pdf",
        "issue": "class=scanned, route=OCR+E2",
    })

    resolved = backlog_manager.resolve_file_a11y(
        COURSE_ID, file_id=None, file_name="lecture-notes.pdf")

    assert resolved == [record["id"]]


def test_resolve_file_a11y_no_match_leaves_findings_unresolved(monkeypatch, tmp_path):
    _use_tmp_plugin_root(monkeypatch, tmp_path)

    backlog_manager.add(COURSE_ID, {
        "finding_type": "file_a11y",
        "detail": "Untagged PDF.",
        "file_name": "111-other.pdf",
        "file_id": 111,
        "issue": "class=digital-untagged, route=E2-insitu",
    })

    resolved = backlog_manager.resolve_file_a11y(COURSE_ID, file_id=222, file_name="nope.pdf")

    assert resolved == []
    assert backlog_manager.list(COURSE_ID)[0]["resolved"] is False
