"""Tests for the APA title-case preflight check wired into preflight_checks.

Guardrail 1: auto title-case lint in preflight.
- check_title_case() emits style.title_case warnings for sentence-case headings.
- check_page() includes title-case findings from heading elements.
- Correctly-cased headings produce no warnings.
"""

import pathlib
import sys

SCRIPTS_DIR = pathlib.Path(__file__).resolve().parents[1] / "scripts"
sys.path.insert(0, str(SCRIPTS_DIR))

import preflight_checks  # noqa: E402


def _criterion_ids(issues):
    return {i.criterion_id for i in issues}


def _title_case_issues(issues):
    return [i for i in issues if i.criterion_id == "style.title_case"]


# ── check_title_case directly ──────────────────────────────────────────────

def test_sentence_case_heading_warns():
    """A heading in sentence case should trigger a style.title_case warning."""
    html = "<h2>this is a sentence case heading</h2>"
    issues = preflight_checks.check_title_case(html)
    assert issues, "Expected at least one issue for sentence-case heading"
    assert all(i.criterion_id == "style.title_case" for i in issues)
    assert all(i.severity == "warning" for i in issues)


def test_correct_title_case_no_warning():
    """A correctly APA-cased heading should produce no title-case warning."""
    html = "<h2>The Rise of Administrative Law</h2>"
    issues = preflight_checks.check_title_case(html)
    tc = _title_case_issues(issues)
    assert tc == [], f"Expected no title-case issues, got: {tc}"


def test_empty_html_no_warning():
    html = ""
    issues = preflight_checks.check_title_case(html)
    assert issues == []


def test_no_heading_no_warning():
    html = "<p>some body text that is not a heading</p>"
    issues = preflight_checks.check_title_case(html)
    tc = _title_case_issues(issues)
    assert tc == []


def test_h3_sentence_case_warns():
    html = "<h3>overview of the module</h3>"
    issues = preflight_checks.check_title_case(html)
    assert issues
    assert issues[0].severity == "warning"
    assert "style.title_case" == issues[0].criterion_id


def test_severity_floor_never_error():
    """Title-case findings must never be 'error' severity."""
    html = "<h1>a bad title that is all lowercase</h1>"
    issues = preflight_checks.check_title_case(html)
    for issue in issues:
        assert issue.severity != "error", "title-case severity must not be error"


def test_faculty_verbatim_body_text_not_flagged():
    """Body prose is not heading text, so it must not trigger title-case lint."""
    # Even if body text is 'sentence case', no title-case warning should fire.
    html = "<p>here is some faculty-authored body text that is all lowercase prose.</p>"
    issues = preflight_checks.check_title_case(html)
    tc = _title_case_issues(issues)
    assert tc == []


# ── check_page integration ─────────────────────────────────────────────────

def test_check_page_includes_title_case_warning_for_sentence_case_h2():
    """check_page() must surface a style.title_case warning for sentence-case h2."""
    html = "<h2>this heading is not title cased</h2><p>body text</p>"
    issues = preflight_checks.check_page(html)
    tc = _title_case_issues(issues)
    assert tc, "check_page should include title-case findings from headings"
    assert all(i.severity == "warning" for i in tc)


def test_check_page_no_title_case_warning_for_correct_heading():
    """check_page() must not emit style.title_case for a correctly-cased heading."""
    html = "<h2>Module Overview: Learning Objectives and Structure</h2><p>body</p>"
    issues = preflight_checks.check_page(html)
    tc = _title_case_issues(issues)
    assert tc == [], f"Unexpected title-case issues: {tc}"
