from db.master_taxonomy_registry import backfill_master_taxonomy_from_products
from db.models import MasterBrand, MasterCollectionRegistry, MasterProduct


def test_backfill_skips_polluted_collection_names(catalog_intent_session):
    session = catalog_intent_session
    session.add_all(
        [
            MasterBrand(code="port-bell", name="Port & Bell", is_active=True),
            MasterCollectionRegistry(
                code="maya-natural-rift-white-oak",
                name="Maya Natural Rift White Oak",
                path_slug="kitchen-cabinets/maya-natural-rift-white-oak",
                is_active=True,
            ),
            MasterProduct(
                sku="GOOD-1",
                name="Good",
                row_hash="1",
                is_active=True,
                category_l1="Kitchen Cabinets",
                brand="Port & Bell",
                collection="Maya Natural Rift White Oak",
            ),
            MasterProduct(
                sku="BAD-1",
                name="Bad",
                row_hash="2",
                is_active=True,
                category_l1="Kitchen Cabinets",
                brand="Port & Bell",
                collection="Natural Rift White Oak",
            ),
        ]
    )
    session.commit()

    result = backfill_master_taxonomy_from_products(session, dry_run=False, allow_invent=True)

    assert result["skipped_polluted"] == 1
    assert (
        session.query(MasterCollectionRegistry)
        .filter(MasterCollectionRegistry.code == "natural-rift-white-oak")
        .one_or_none()
        is None
    )
