from magento.payload_mapper import (
    _compute_parent_neutral_name,
    is_polluted_product_title,
    resolve_product_display_name,
    snapshot_to_magento_product,
)


def test_parent_neutral_name_ignores_variation_option_label():
    row = {
        "name": "Maya Snow White Wall End",
        "configurable_variations": (
            "sku=C1,height=30,variation_option_label=12 in W x 30 in H x 12 in D|"
            "sku=C2,height=42,variation_option_label=12 in W x 42 in H x 12 in D"
        ),
        "configurable_attributes": "height,variation_option_label",
    }
    rows_by_sku = {
        "PARENT": row,
        "C1": {"sku": "C1"},
        "C2": {"sku": "C2"},
    }
    neutral = _compute_parent_neutral_name(row, {"height", "variation_option_label"}, rows_by_sku)
    assert neutral == "Maya Snow White Wall End (Height 30–42)"
    assert "variation_option_label" not in (neutral or "")


def test_parent_payload_sets_url_key_from_clean_title():
    row = {
        "sku": "MSW-WECA-PARENT",
        "name": "Maya Snow White Wall End (Height 30–42)",
        "seo_title": "Maya Snow White Wall End",
        "product_type": "configurable",
        "configurable_variations": "sku=C1,height=30|sku=C2,height=42",
        "configurable_attributes": "height",
        "variant_list": "C1,C2",
    }
    rows_by_sku = {"MSW-WECA-PARENT": row, "C1": {"sku": "C1"}, "C2": {"sku": "C2"}}
    payload = snapshot_to_magento_product(row, rows_by_sku=rows_by_sku)
    url_keys = [
        a["value"]
        for a in (payload.get("custom_attributes") or [])
        if a.get("attribute_code") == "url_key"
    ]
    assert url_keys == ["maya-snow-white-wall-end-msw-weca-parent"]


def test_configurable_parent_visibility_catalog_search_child_not_visible():
    parent = {
        "sku": "MSW-WECA-PARENT",
        "name": "Maya Snow White Wall End",
        "product_type": "configurable",
        "visibility": "Not Visible Individually",
        "configurable_variations": "sku=C1,height=30|sku=C2,height=42",
        "variant_list": "C1,C2",
    }
    child = {
        "sku": "C1",
        "name": "Child",
        "product_type": "simple",
        "variant_of": "MSW-WECA-PARENT",
        "visibility": "Catalog, Search",
    }
    rows_by_sku = {"MSW-WECA-PARENT": parent, "C1": child}
    parent_payload = snapshot_to_magento_product(parent, rows_by_sku=rows_by_sku)
    child_payload = snapshot_to_magento_product(child, rows_by_sku=rows_by_sku)
    assert parent_payload["visibility"] == 4
    assert child_payload["visibility"] == 1


def test_master_status_active_stays_top_level_int_not_custom_attr():
    """Master catalog status='active' must not leak into Magento custom_attributes."""
    row = {
        "sku": "PB-MW-DB12",
        "name": "Cab MW Base Drawer 12w",
        "product_type": "simple",
        "status": "active",
        "visibility": "Catalog, Search",
        "additional_attributes": "status=active,color=White",
        "color": "White",
    }
    payload = snapshot_to_magento_product(row)
    assert payload["status"] == 1
    assert payload["visibility"] == 4
    codes = {
        str(a.get("attribute_code") or "").lower()
        for a in (payload.get("custom_attributes") or [])
    }
    assert "status" not in codes
    assert "visibility" not in codes
    assert "color" in codes


def test_stale_meta_title_does_not_override_master_name():
    row = {
        "sku": "ACH-B12",
        "name": '12"W x 34.5"H Single Door Base Cabinet (B12) - Anna Caramel Harvest | Port & Bell',
        "meta_title": "B12 - Anna Caramel Harvest - Port & Bell - Base -- Assembled --",
        "product_type": "simple",
        "variant_of": "ACH-B-PARENT",
        "product_url_slug": "b12-anna-caramel-harvest-port-bell-base-unassembled",
    }
    payload = snapshot_to_magento_product(row)
    assert payload["name"].startswith('12"W')
    assert "Assembled" not in payload["name"]
    url_keys = [
        a["value"]
        for a in (payload.get("custom_attributes") or [])
        if a.get("attribute_code") == "url_key"
    ]
    assert url_keys == [
        "12-w-x-34-5-h-single-door-base-cabinet-b12-anna-caramel-harvest-port-bell-ach-b12"
    ]


def test_polluted_meta_title_does_not_become_parent_name_or_url():
    row = {
        "sku": "ACH-B-PARENT",
        "name": '12"-21"W x 34.5"H Single Door Base Cabinet - Anna Caramel Harvest | Port & Bell (4 Sizes)',
        "meta_title": "Anna Caramel Harvest {variation_option_label (12 in W x 34.5 in H x 24 in D), width 12-21}",
        "product_type": "configurable",
        "product_url_slug": "anna-caramel-harvest-variation-option-label-12-in-w",
        "configurable_variations": "sku=ACH-B12,width=12|sku=ACH-B15,width=15",
        "configurable_attributes": "width",
        "variant_list": "ACH-B12,ACH-B15",
    }
    rows_by_sku = {"ACH-B-PARENT": row, "ACH-B12": {"sku": "ACH-B12"}, "ACH-B15": {"sku": "ACH-B15"}}
    payload = snapshot_to_magento_product(row, rows_by_sku=rows_by_sku)
    assert "variation_option_label" not in payload["name"]
    assert payload["name"].startswith('12"')
    assert payload["visibility"] == 4
    url_keys = [
        a["value"]
        for a in (payload.get("custom_attributes") or [])
        if a.get("attribute_code") == "url_key"
    ]
    assert url_keys
    assert "variation-option-label" not in url_keys[0]
    assert is_polluted_product_title(row["meta_title"])
    assert resolve_product_display_name(row).startswith('12"')