from app.jobs.audit_storefront_filter_readiness import audit_storefront_filter_readiness
from db.models import MasterProduct, MasterProductAttributeValue, MasterProductRelation


def _attr(product, code, value):
    return MasterProductAttributeValue(
        product_id=product.id,
        sku=product.sku,
        attribute_code=code,
        value=value,
    )


def test_audit_storefront_filter_readiness_normalizes_left_pane_values(catalog_intent_session):
    session = catalog_intent_session
    product = MasterProduct(
        sku="ACH-B12",
        name="B12 - Anna Caramel Harvest",
        brand="Port & Bell",
        manufacturer="Port & Bell",
        product_family="Cabinets",
        collection="Anna Caramel Harvest",
        assembly_type="RTA",
        item_size="B12",
        is_active=True,
        row_hash="ach-b12",
    )
    session.add(product)
    session.flush()
    session.add_all(
        [
            _attr(product, "grab_n_go", "Yes"),
            _attr(product, "cabinet_door_style", "raised panel"),
            _attr(product, "cabinet_door_overlay", "Full Overlay"),
            _attr(product, "cabinet_construction_type", "Framed Construction"),
            _attr(product, "variation_width_in", '12"'),
            _attr(product, "variation_height_in", '34.5"'),
            _attr(product, "variation_depth_in", '24"'),
            _attr(product, "cabinet_type_sub_category_1", "Base"),
            _attr(product, "cabinet_type_sub_category_2", "Drawer Base Cabinet"),
            _attr(product, "drawer_slide_glide", "Undermount Full Extension Soft Close"),
            _attr(product, "door_hinge_type", "Concealed 6-Way Adjustable Soft Close"),
            _attr(product, "drawer_box_material", "Solid Wood"),
            _attr(product, "drawer_box_construction", "Dovetail"),
            _attr(product, "cabinet_shelf_clip_type", "Stainless Steel Locking"),
            _attr(product, "face_frame_material", '3/4" x 1 1/2" Solid Wood'),
            _attr(product, "drawer_box_bottom_material", "Plywood"),
            _attr(product, "collection_color", "Stone Gray"),
            _attr(product, "number_of_shelves_included", "1"),
        ]
    )
    session.commit()

    report = audit_storefront_filter_readiness(session, limit=50)
    by_code = {row["code"]: row for row in report["filters"]}

    assert report["ready"] is True
    assert by_code["item_availability"]["top_values"][0][0] == "Grab & Go"
    assert by_code["door_style"]["top_values"][0][0] == "Raised Panel"
    assert by_code["color_finish"]["top_values"][0][0] == "Stone Gray"
    assert by_code["overlay"]["top_values"][0][0] == "Full Overlay - Framed"
    assert by_code["drawer_slides"]["top_values"][0][0] == "Undermount Slides"
    assert by_code["hinges"]["top_values"][0][0] == "Concealed Soft-Close Hinges"
    assert by_code["drawer_box"]["top_values"][0][0] == "Dovetail Solid Wood"
    assert by_code["shelf_clips"]["top_values"][0][0] == "Stainless Steel (locking)"
    assert by_code["width"]["top_values"][0][0] == "12"
    assert by_code["height"]["top_values"][0][0] == "34.5"
    assert by_code["depth"]["top_values"][0][0] == "24"
    assert by_code["soft_close"]["top_values"][0][0] == "Yes"
    assert by_code["cabinet_type"]["top_values"][0][0] == "base cabinets"
    assert by_code["base_cabinets"]["top_values"][0][0] == "drawer base cabinet"


def test_audit_storefront_filter_readiness_flags_invalid_values_and_excludes_parents(catalog_intent_session):
    session = catalog_intent_session
    parent = MasterProduct(
        sku="ACH-B-PARENT",
        name="Parent",
        collection="Anna Caramel Harvest",
        is_active=True,
        row_hash="parent",
    )
    child = MasterProduct(
        sku="ACH-W2424",
        name="Wall Cabinet",
        brand="Port & Bell",
        manufacturer="Port & Bell",
        product_family="Cabinets",
        collection="Anna Caramel Harvest",
        is_active=True,
        row_hash="child",
    )
    session.add_all([parent, child])
    session.flush()
    session.add(
        MasterProductRelation(
            parent_sku=parent.sku,
            child_sku=child.sku,
            sort_order=1,
            option_mapping={"width": "24"},
        )
    )
    session.add_all(
        [
            _attr(child, "item_availability", "Maybe"),
            _attr(child, "cabinet_door_style", "Mystery Style"),
            _attr(child, "variation_width_in", "abc"),
            _attr(child, "variation_height_in", "30"),
            _attr(child, "variation_depth_in", "12"),
            _attr(child, "cabinet_type_sub_category_1", "Wall"),
            _attr(child, "cabinet_type_sub_category_2", "Corner Wall Cabinet"),
        ]
    )
    session.commit()

    report = audit_storefront_filter_readiness(
        session,
        attributes=["item_availability", "door_style", "width", "wall_cabinets"],
    )
    by_code = {row["code"]: row for row in report["filters"]}

    assert report["active_sellable_product_count"] == 1
    assert report["ready"] is False
    assert by_code["item_availability"]["invalid_value_count"] == 1
    assert by_code["door_style"]["invalid_value_count"] == 1
    assert by_code["width"]["missing_count"] == 1
    assert by_code["wall_cabinets"]["top_values"][0][0] == "corner wall cabinet"
