from unittest.mock import MagicMock, patch

from db.collection_sku_mapping import sync_collection_reconcile_channels


def test_sync_collection_reconcile_channels_blocks_shopify_when_magento_aborts():
    session = MagicMock()
    master_skus = [f"SKU-{index}" for index in range(3)]
    magento_result = {
        "channel_code": "magento",
        "errors": [{"path_slug": "kitchen/test", "error": "boom", "aborted": True}],
        "results": [],
    }

    with patch(
        "db.collection_landing_push.sync_magento_collection_products_channel",
        return_value=magento_result,
    ) as magento_sync:
        with patch("shopify.collection_sync.assign_product_to_collections") as shopify_assign:
            result = sync_collection_reconcile_channels(
                session,
                path_slug="kitchen/test",
                master_skus=master_skus,
                magento_connection_id=1,
                shopify_connection_id=1,
                sync_magento=True,
                sync_shopify=True,
            )

    magento_sync.assert_called_once()
    assert magento_sync.call_args.kwargs["master_skus"] == master_skus
    shopify_assign.assert_not_called()
    assert result["shopify"]["skipped"] is True
    assert result["shopify"]["reason"] == "magento_sync_incomplete"


def test_sync_collection_reconcile_channels_runs_shopify_after_magento_completes():
    session = MagicMock()
    master_skus = ["ACH-B12", "ACH-W15"]
    magento_result = {
        "channel_code": "magento",
        "errors": [],
        "results": [
            {
                "product_sync": {
                    "processed": 2,
                    "assigned": 1,
                    "failed": 0,
                }
            }
        ],
    }

    with patch(
        "db.collection_landing_push.sync_magento_collection_products_channel",
        return_value=magento_result,
    ):
        with patch("db.channel_sku_mapping.mapping_by_master", return_value={"ACH-B12": "ACH-B12", "ACH-W15": "ACH-W15"}):
            with patch("db.models.ShopifyConnection"):
                with patch("shopify.connections.build_client", return_value=MagicMock()):
                    with patch("shopify.product_sync._lookup_product_id", return_value="gid://shopify/Product/1"):
                        with patch(
                            "shopify.collection_sync.assign_product_to_collections",
                            return_value=None,
                        ) as shopify_assign:
                            result = sync_collection_reconcile_channels(
                                session,
                                path_slug="kitchen/test",
                                master_skus=master_skus,
                                magento_connection_id=1,
                                shopify_connection_id=1,
                                sync_magento=True,
                                sync_shopify=True,
                            )

    assert shopify_assign.call_count == 2
    assert result["shopify"]["assigned"] == 2
