from db.channel_remote_attributes import list_all_master_attribute_values
from db.collection_path_guard import canonical_collection_option_labels
from db.master_taxonomy_merge import merge_taxonomy_nodes
from db.master_taxonomy_registry import upsert_collection_registry
from db.master_taxonomy_sync import upsert_taxonomy_node
from db.models import MasterProduct


def test_collection_option_labels_prefer_canonical_not_raw_product_strings(catalog_intent_session):
    session = catalog_intent_session
    hub = upsert_taxonomy_node(
        session,
        {"name": "Kitchen Cabinets", "path_slug": "kitchen-cabinets", "node_kind": "hub"},
        allow_invent=True,
    )["node"]
    canonical = upsert_taxonomy_node(
        session,
        {
            "name": "Anna Snow White",
            "path_slug": "kitchen-cabinets/anna-snow-white",
            "parent_id": hub["id"],
            "node_kind": "collection",
        },
        allow_invent=True,
    )["node"]
    alias = upsert_taxonomy_node(
        session,
        {
            "name": "Snow White",
            "path_slug": "kitchen-cabinets/snow-white",
            "parent_id": hub["id"],
            "node_kind": "collection",
        },
        allow_invent=True,
    )["node"]
    upsert_taxonomy_node(
        session,
        {
            "name": "Black Rift White Oak",
            "path_slug": "kitchen-cabinets/black-rift-white-oak",
            "parent_id": hub["id"],
            "node_kind": "collection",
        },
        allow_invent=True,
    )
    session.flush()
    merge_taxonomy_nodes(
        session,
        canonical_node_id=canonical["id"],
        alias_node_ids=[alias["id"]],
        dry_run=False,
    )
    upsert_collection_registry(
        session,
        {
            "code": "anna-snow-white",
            "name": "Anna Snow White",
            "path_slug": "kitchen-cabinets/anna-snow-white",
            "is_active": True,
        },
        allow_invent=True,
    )
    upsert_collection_registry(
        session,
        {
            "code": "black-rift-white-oak",
            "name": "Black Rift White Oak",
            "path_slug": "kitchen-cabinets/black-rift-white-oak",
            "is_active": True,
        },
        allow_invent=True,
    )
    session.add_all(
        [
            MasterProduct(
                sku="ASW-B12",
                name="Base",
                category_l1="Kitchen Cabinets",
                collection="Snow White",
                row_hash="h1",
                is_active=True,
            ),
            MasterProduct(
                sku="BRWO-ACC-1",
                name="Accessory",
                category_l1="Kitchen Cabinets",
                collection="Black Rift White Oak - Accessories",
                row_hash="h2",
                is_active=True,
            ),
            MasterProduct(
                sku="VT-1",
                name="Top",
                category_l1="Kitchen Cabinets",
                collection="Vanity Tops",
                row_hash="h3",
                is_active=True,
            ),
        ]
    )
    session.flush()

    labels = canonical_collection_option_labels(session, category_l1="Kitchen Cabinets")
    assert "Anna Snow White" in labels
    assert "Black Rift White Oak" in labels
    assert "Snow White" not in labels
    assert "Black Rift White Oak - Accessories" not in labels
    assert "Vanity Tops" not in labels

    via_list = list_all_master_attribute_values(session, "collection")
    assert "Anna Snow White" in via_list
    assert "Snow White" not in via_list
    assert "Black Rift White Oak - Accessories" not in via_list
