from backend.text_coverage import (text_coverage, segment_text_layer,
                                   fallback_objects, merge_objects,
                                   recover_sheet_number)


def test_coverage_full():
    text = "NOTE 1\nALL LUMBER NO. 2 SOUTHERN PINE\nNOTE 2\nUSE 5/8\" PLYWOOD"
    objects = [{"source_text": "ALL LUMBER NO. 2 SOUTHERN PINE"},
               {"source_text": "USE 5/8\" PLYWOOD"}]
    cov = text_coverage(text, objects)
    assert cov["covered_lines"] == 2
    assert cov["total_lines"] == 2
    assert cov["ratio"] == 1.0


def test_coverage_zero_on_empty_objects():
    cov = text_coverage("LINE ALPHA CONTENT\nLINE BETA CONTENT\nLINE GAMMA CONTENT", [])
    assert cov["ratio"] == 0.0 and cov["total_lines"] == 3


def test_coverage_ignores_short_and_numeric_noise_lines():
    text = "15\"\n19\"\nA\nB\nREAL NOTE ABOUT FRAMING HERE"
    cov = text_coverage(text, [{"source_text": "REAL NOTE ABOUT FRAMING HERE"}])
    assert cov["total_lines"] == 1 and cov["ratio"] == 1.0


def test_segment_notes_and_rows():
    text = "WOOD CONSTRUCTION\n1. \nALL SAWN LUMBER TO BE SOUTHERN PINE.\n2. \nROOF SHEATHING 5/8\" PLYWOOD."
    segs = segment_text_layer(text)
    assert any("ALL SAWN LUMBER" in s for s in segs)
    assert any("ROOF SHEATHING" in s for s in segs)


def test_fallback_objects_verbatim_and_stamped():
    objs = fallback_objects("1. \nALL SAWN LUMBER TO BE SOUTHERN PINE.", page_number=8)
    assert len(objs) == 1
    assert objs[0]["source_text"] == "1 ALL SAWN LUMBER TO BE SOUTHERN PINE."
    assert objs[0]["grounding"] == "text_layer_fallback"
    assert objs[0]["confidence"] == "low"


def test_merge_objects_keeps_vision_and_unions_text():
    vision = [
        {"source_text": "2X6 WD STUD @ 16\" O.C.", "object_type": "wall"},
        {"source_text": None, "graphical_basis": "light fixture symbol, grid C-4",
         "object_type": "lighting_fixture"},
    ]
    text = [
        {"source_text": "2X6 WD STUD @ 16\" O.C.", "object_type": "wall"},
        {"source_text": "ALL LUMBER NO. 2 SOUTHERN PINE", "object_type": "general_note"},
    ]
    merged = merge_objects(vision, text)
    assert len(merged) == 3
    assert any(o.get("graphical_basis") for o in merged)
    assert merged[0]["object_type"] == "wall"


def test_merge_objects_dedupes_by_normalized_text():
    a = [{"source_text": "RTU-1: 5 TON, 1600 CFM"}]
    b = [{"source_text": "rtu 1 5 ton 1600 cfm"}]
    assert len(merge_objects(a, b)) == 1


def test_recover_sheet_number_from_title_block():
    text = ("WALL SECTIONS\n...\nSheet Information\nS301\n"
            "Issue Date 05.29.26\nProject Number 25177")
    assert recover_sheet_number(text) == "S301"


def test_recover_sheet_number_none_when_absent():
    assert recover_sheet_number("just some notes about lumber") is None


def test_recover_prefers_discipline_pattern_over_dates():
    text = "Issue Date 05.29.26\nProject Number 25177\nA102 REFLECTED CEILING PLAN"
    assert recover_sheet_number(text) == "A102"
