from db.brochure_pdf_taxonomy import extract_taxonomy_groups_from_text


def test_extract_taxonomy_groups_from_text_classifies_anna_wall_and_base_patterns():
    text = """
    STACKED WALL*
    GDW1215 GDW1515 GDW1815 GDW2115 GDW2415 GDW2715 GDW3015 GDW3315 GDW3615 GDWCOR2415
    BASIC WALL*
    W0930 W1230 W1530 W1830 W2130 GDW1530 GDW1830
    W2430 W2730 W3030 W3330 W3630 W3930 GDW3030 GDW3630
    BRIDGE WALL
    W2412 W3012 W3612 W2415 W3015 W3315 W3615 W2418 W3018 W3618 W2424 W3024 W3624
    CORNER WALL
    WBC2730 WBC2736 WBC2742 WDC2430 WDC2436 WDC2442 WCOR2430 WCOR2436 WCOR2442 GDWDC2430 GDWDC2436 GDWDC2442
    BASIC BASES
    B12 B15 B18 B21 B24 B27 B30 B33 B36 B39 BH09 BH12 BH15 BH18 BH21 BH24
    SINK BASES
    SB24 SB27 SB30 SBF30 SBF36 CSB36-FP
    DRAWER BASES
    DB30-2 DB12-3 DB15-3 DB18-3
    """

    groups = extract_taxonomy_groups_from_text(text)
    by_leaf = {
        (group["brochure_l1_label"], group.get("brochure_l2_label"), group.get("brochure_leaf_label")): set(group["skus"])
        for group in groups
    }

    assert ("Wall Cabinets", "Stacked Wall", None) in by_leaf
    assert "GDW1215" in by_leaf[("Wall Cabinets", "Stacked Wall", None)]
    assert ("Wall Cabinets", "Basic Wall", None) in by_leaf
    assert "W1230" in by_leaf[("Wall Cabinets", "Basic Wall", None)]
    assert ("Wall Cabinets", "Double Door Glass Wall Cabinet", None) in by_leaf
    assert "GDW3030" in by_leaf[("Wall Cabinets", "Double Door Glass Wall Cabinet", None)]
    assert ("Wall Cabinets", "Single Door Glass Wall Cabinet", None) in by_leaf
    assert "GDW1530" in by_leaf[("Wall Cabinets", "Single Door Glass Wall Cabinet", None)]
    assert ("Wall Cabinets", "Bridge Wall", None) in by_leaf
    assert "W3015" in by_leaf[("Wall Cabinets", "Bridge Wall", None)]
    assert ("Wall Cabinets", "Corner Wall", "Blind Corner Wall Cabinet") in by_leaf
    assert "WBC2730" in by_leaf[("Wall Cabinets", "Corner Wall", "Blind Corner Wall Cabinet")]
    assert ("Wall Cabinets", "Corner Wall", "Diagonal Corner Glass Wall Cabinet") in by_leaf
    assert "GDWDC2430" in by_leaf[("Wall Cabinets", "Corner Wall", "Diagonal Corner Glass Wall Cabinet")]
    assert ("Base Cabinets", "Basic Bases", "Single Door") in by_leaf
    assert "BH09" in by_leaf[("Base Cabinets", "Basic Bases", "Single Door")]
    assert ("Base Cabinets", "Basic Bases", "Double Door Base Cabinet") in by_leaf
    assert "B30" in by_leaf[("Base Cabinets", "Basic Bases", "Double Door Base Cabinet")]
    assert ("Base Cabinets", "Sink Bases", "Sink Base") in by_leaf
    assert "SBF30" in by_leaf[("Base Cabinets", "Sink Bases", "Sink Base")]
    assert ("Base Cabinets", "Drawer Bases", "Drawer Base") in by_leaf
    assert "DB30-2" in by_leaf[("Base Cabinets", "Drawer Bases", "Drawer Base")]


def test_extract_taxonomy_groups_from_text_filters_touch_up_and_classifies_accessories():
    text = """
    TOUCH UP KIT
    SPRAY CAN PINT OF PAINT QUART OF PAINT GALLON OF PAINT
    ACCESSORIES
    SC10 GD-W1530 ROTD-15 FS24 SD
    MOLDINGS
    TK8 OCM8-1 SHM8
    """

    groups = extract_taxonomy_groups_from_text(text)
    by_leaf = {
        (group["brochure_l1_label"], group.get("brochure_l2_label"), group.get("brochure_leaf_label")): set(group["skus"])
        for group in groups
    }

    assert ("Accessories", None, "Items") in by_leaf
    assert {"SC10", "ROTD-15", "FS24", "SD"}.issubset(by_leaf[("Accessories", None, "Items")])
    assert ("Accessories", "Loose Glass Door", None) in by_leaf
    assert "GD-W1530" in by_leaf[("Accessories", "Loose Glass Door", None)]
    assert ("Mouldings", "Toe Kick", None) in by_leaf
    assert "TK8" in by_leaf[("Mouldings", "Toe Kick", None)]
    assert ("Mouldings", "Outside Corner", None) in by_leaf
    assert "OCM8-1" in by_leaf[("Mouldings", "Outside Corner", None)]
    assert ("Mouldings", "Shoe", None) in by_leaf
    assert "SHM8" in by_leaf[("Mouldings", "Shoe", None)]
    all_skus = {sku for skus in by_leaf.values() for sku in skus}
    assert "SPRAY" not in all_skus
