"""SET_RELATIONS must set child axis attrs before Magento addChild."""

from __future__ import annotations

from unittest.mock import MagicMock

from magento.sync_service import MagentoSyncService


def _svc_with_height_options(api: MagicMock) -> MagentoSyncService:
    attr_repo = MagicMock()
    attr_repo.get_options.return_value = {
        "30": 696,
        "36": 708,
        "42": 702,
        "34.5": 681,
    }
    attr_repo.get_attributes.return_value = {
        "height": {"attribute_id": 155, "frontend_label": "Height"},
    }
    return MagentoSyncService(
        api,
        {"id": 4},
        attr_cache_repo=attr_repo,
    )


def test_ensure_child_axis_attrs_puts_missing_height():
    api = MagicMock()
    api.get_product.return_value = {
        "sku": "ASW-MWEP30",
        "custom_attributes": [],
    }
    api.put_product.return_value = (200, {"sku": "ASW-MWEP30"})

    svc = _svc_with_height_options(api)
    err = svc._ensure_child_axis_attributes_on_magento(
        "ASW-MWEP30",
        attr_codes=["height"],
        mappings=[{"sku": "ASW-MWEP30", "height": "30"}],
        child_to_mapping={"ASW-MWEP30": {"height": "30"}},
        rows_by_sku={"ASW-MWEP30": {"sku": "ASW-MWEP30"}},
    )

    assert err is None
    api.put_product.assert_called_once()
    payload = api.put_product.call_args[0][1]
    assert payload["sku"] == "ASW-MWEP30"
    assert payload["custom_attributes"] == [{"attribute_code": "height", "value": "696"}]


def test_ensure_child_axis_attrs_skips_when_already_set():
    api = MagicMock()
    api.get_product.return_value = {
        "sku": "ASW-MWEP30",
        "custom_attributes": [{"attribute_code": "height", "value": "696"}],
    }

    svc = _svc_with_height_options(api)
    err = svc._ensure_child_axis_attributes_on_magento(
        "ASW-MWEP30",
        attr_codes=["height"],
        mappings=[],
        child_to_mapping={"ASW-MWEP30": {"height": "30"}},
        rows_by_sku={},
    )

    assert err is None
    api.put_product.assert_not_called()


def test_link_configurable_child_ensures_height_then_links():
    api = MagicMock()
    api.get_product.return_value = {"sku": "ASW-MWEP30", "custom_attributes": []}
    api.put_product.return_value = (200, {"sku": "ASW-MWEP30"})
    api.add_configurable_child.return_value = (200, None)

    svc = _svc_with_height_options(api)
    ok = svc._link_configurable_child(
        "ASW-MWEP-PARENT",
        "ASW-MWEP30",
        attr_codes=["height"],
        mappings=[{"sku": "ASW-MWEP30", "height": "30"}],
        child_to_mapping={"ASW-MWEP30": {"height": "30"}},
        rows_by_sku={"ASW-MWEP30": {"sku": "ASW-MWEP30"}},
    )

    assert ok is True
    api.put_product.assert_called_once()
    api.add_configurable_child.assert_called_once_with("ASW-MWEP-PARENT", "ASW-MWEP30")


def test_link_configurable_child_retries_after_missing_attr_error():
    api = MagicMock()
    api.get_product.return_value = {"sku": "ASW-MWEP36", "custom_attributes": []}
    api.put_product.return_value = (200, {"sku": "ASW-MWEP36"})
    api.add_configurable_child.side_effect = [
        (400, 'The child product doesn\'t have the "height" attribute value.'),
        (200, None),
    ]

    svc = _svc_with_height_options(api)
    ok = svc._link_configurable_child(
        "ASW-MWEP-PARENT",
        "ASW-MWEP36",
        attr_codes=["height"],
        mappings=[{"sku": "ASW-MWEP36", "height": "36"}],
        child_to_mapping={"ASW-MWEP36": {"height": "36"}},
        rows_by_sku={"ASW-MWEP36": {"sku": "ASW-MWEP36"}},
    )

    assert ok is True
    assert api.put_product.call_count >= 1
    assert api.add_configurable_child.call_count == 2


def test_resolve_child_axis_falls_back_to_sku_derived_height():
    api = MagicMock()
    svc = _svc_with_height_options(api)

    resolved = svc._resolve_child_axis_value_indexes(
        "ASW-MWEP42",
        attr_codes=["height"],
        mappings=[],
        child_to_mapping={},
        rows_by_sku={"ASW-MWEP42": {"sku": "ASW-MWEP42"}},
    )

    assert resolved == {"height": "702"}
