from db.product_taxonomy_workspace import (
    canonical_channel_taxonomies_for_sku,
    collapse_to_leaf_channel_taxonomies,
    filter_skus_without_channel_products,
)


def test_collapse_magento_category_paths_to_leaf_assignments():
    items = [
        {"channel_code": "magento", "taxonomy_kind": "category", "label": "Default Category", "remote_id": "2"},
        {"channel_code": "magento", "taxonomy_kind": "category", "label": "Default Category/All Products", "remote_id": "4"},
        {"channel_code": "magento", "taxonomy_kind": "category", "label": "Default Category/Kitchen Cabinets", "remote_id": "5"},
        {"channel_code": "magento", "taxonomy_kind": "category", "label": "Default Category/Kitchen Cabinets/Base", "remote_id": "214"},
        {
            "channel_code": "magento",
            "taxonomy_kind": "category",
            "label": "Default Category/Kitchen Cabinets/Base/Single Door Base Cabinet",
            "remote_id": "215",
        },
        {
            "channel_code": "magento",
            "taxonomy_kind": "category",
            "label": "Default Category/Kitchen Cabinets/Port & Bell",
            "remote_id": "144",
        },
        {
            "channel_code": "magento",
            "taxonomy_kind": "category",
            "label": "Default Category/Kitchen Cabinets/Port & Bell/Anna Caramel Harvest",
            "remote_id": "145",
        },
        {"channel_code": "magento", "taxonomy_kind": "category", "label": "Default Category/Anna Caramel Harvest", "remote_id": "268"},
    ]
    leaf = collapse_to_leaf_channel_taxonomies(items)
    assert len(leaf) == 4
    labels = {row["label"] for row in leaf}
    assert "Default Category/All Products" in labels
    assert "Default Category/Kitchen Cabinets/Base/Single Door Base Cabinet" in labels
    assert "Default Category/Kitchen Cabinets/Port & Bell/Anna Caramel Harvest" in labels
    assert "Default Category/Anna Caramel Harvest" in labels


def test_collapse_dedupes_same_default_category_across_connections():
    """UI bug: M: Default Category ×3 when remote_id=2 exists per Magento connection."""
    items = [
        {
            "channel_code": "magento",
            "taxonomy_kind": "category",
            "label": "Default Category",
            "remote_id": "2",
            "connection_id": None,
        },
        {
            "channel_code": "magento",
            "taxonomy_kind": "category",
            "label": "Default Category",
            "remote_id": "2",
            "connection_id": 1,
        },
        {
            "channel_code": "magento",
            "taxonomy_kind": "category",
            "label": "Default Category",
            "remote_id": "2",
            "connection_id": 4,
        },
    ]
    leaf = collapse_to_leaf_channel_taxonomies(items)
    assert len(leaf) == 1
    assert leaf[0]["remote_id"] == "2"
    assert leaf[0]["label"] == "Default Category"


def test_filter_skus_without_channel_products(monkeypatch, catalog_intent_session):
    session = catalog_intent_session

    def _on_channel(session, channel_code, master_skus, *, connection_id):
        if channel_code == "magento":
            return {"ON-M"}
        if channel_code == "shopify":
            return {"ON-S"}
        return set()

    monkeypatch.setattr("db.product_taxonomy_workspace.master_skus_on_channel", _on_channel)

    filtered = filter_skus_without_channel_products(
        session,
        ["ON-M", "OFF-M", "ON-S", "OFF-ALL"],
        without_magento=True,
        without_shopify=True,
        magento_connection_id=1,
        shopify_connection_id=1,
    )
    assert filtered == ["OFF-M", "OFF-ALL"]


def test_canonical_channel_taxonomies_prefer_resolved_targets_over_stale_assignments(
    monkeypatch,
    catalog_intent_session,
):
    session = catalog_intent_session

    monkeypatch.setattr(
        "db.product_taxonomy_workspace.resolve_magento_category_targets",
        lambda *args, **kwargs: ([157], ["Default Category/Kitchen Cabinets/Wall Cabinets/Single Door Glass Wall"]),
    )
    monkeypatch.setattr(
        "db.product_taxonomy_workspace.resolve_shopify_collection_ids",
        lambda *args, **kwargs: ["gid://shopify/Collection/42"],
    )

    items = canonical_channel_taxonomies_for_sku(
        session,
        "ACH-GDW2415",
        canonical_fields={"name": "Test Product"},
        magento_labels={"157": "Default Category/Kitchen Cabinets/Wall Cabinets/Single Door Glass Wall"},
        shopify_labels={"gid://shopify/Collection/42": "Anna Caramel Harvest"},
        magento_connection_id=1,
        shopify_connection_id=2,
    )

    assert items == [
        {
            "channel_code": "magento",
            "taxonomy_kind": "category",
            "remote_id": "157",
            "remote_path": "Default Category/Kitchen Cabinets/Wall Cabinets/Single Door Glass Wall",
            "label": "Default Category/Kitchen Cabinets/Wall Cabinets/Single Door Glass Wall",
            "connection_id": 1,
        },
        {
            "channel_code": "shopify",
            "taxonomy_kind": "collection",
            "remote_id": "gid://shopify/Collection/42",
            "remote_path": None,
            "label": "Anna Caramel Harvest",
            "connection_id": 2,
        },
    ]
