from db.collection_filter_profile_seed import extract_brochure_filter_profile, seed_collection_filter_profiles
from db.models import MasterCollectionRegistry


def test_extract_brochure_filter_profile_maps_brochure_specs():
    payload = {
        "source_document": {
            "collection_code": "ASW",
            "file_name": "AnnaSnowWhite_0226.pdf",
            "source_url": "https://example.com/AnnaSnowWhite_0226.pdf",
        },
        "collection": {
            "display_name": "Anna Snow White",
            "color_label": "Snow White",
        },
        "specifications": {
            "style": {
                "door_style": "Shaker",
                "finish_type": "Painted Finish",
                "overlay": "Full Overlay",
                "cabinet_frame_style": "Framed",
                "drawer_front_style": "5-Piece Front",
                "wood_species": "Birch",
            },
            "construction": {
                "cabinet_box": {
                    "side_panel_material": "Plywood",
                }
            },
            "hardware_features": {
                "door_hinges": {"soft_close": True},
                "drawer_slides": {"soft_close": True},
            },
            "cabinet_box_finish": "White painted finish",
        },
    }

    result = extract_brochure_filter_profile(payload)

    assert result["collection_code"] == "ASW"
    assert result["filter_attributes"]["door_style"] == "Shaker"
    assert result["filter_attributes"]["color"] == "White"
    assert result["filter_attributes"]["finish"] == "Painted"
    assert result["filter_attributes"]["cabinet_construction"] == "Framed"
    assert result["filter_attributes"]["cabinet_door_overlay"] == "Full Overlay"
    assert result["filter_attributes"]["wood_species"] == "Birch"
    assert result["filter_attributes"]["drawer_front_style"] == "5-Piece Front"
    assert result["filter_attributes"]["soft_close"] == "Yes"
    assert result["filter_attributes"]["cabinet_box_finish"] == "White painted finish"
    assert result["filter_attributes"]["carcass_material"] == "Plywood"


def test_seed_collection_filter_profiles_updates_registry_aliases(catalog_intent_session):
    registry = MasterCollectionRegistry(
        code="ACH",
        name="Anna Caramel Harvest",
        path_slug="kitchen-cabinets/anna-caramel-harvest",
        aliases={"filter_attributes": {"door_style": "Old Style"}},
        is_active=True,
    )
    catalog_intent_session.add(registry)
    catalog_intent_session.commit()

    payload = {
        "source_document": {"collection_code": "ACH", "file_name": "AnnaCaramelHarvest_0226.pdf"},
        "collection": {"display_name": "Anna Caramel Harvest", "color_label": "Caramel Harvest"},
        "specifications": {
            "style": {
                "door_style": "Shaker",
                "finish_type": "Stained",
                "overlay": "Full Overlay",
                "cabinet_frame_style": "Framed",
            }
        },
    }

    result = seed_collection_filter_profiles(catalog_intent_session, [payload], dry_run=False)
    catalog_intent_session.commit()
    catalog_intent_session.refresh(registry)

    assert result["updated"] == 1
    assert registry.aliases["filter_attributes"]["door_style"] == "Shaker"
    assert registry.aliases["filter_attributes"]["color"] == "Brown"
    assert registry.aliases["filter_attribute_sources"]["door_style"] == "specifications.style.door_style"
    assert registry.aliases["brochure_filter_attributes"]["cabinet_door_overlay"] == "Full Overlay"
    assert registry.aliases["filter_attributes"]["finish"] == "Stained"


def test_seed_collection_filter_profiles_can_preserve_existing_values(catalog_intent_session):
    registry = MasterCollectionRegistry(
        code="GRACE",
        name="Grace Snow White",
        path_slug="kitchen-cabinets/grace-snow-white",
        aliases={"filter_attributes": {"cabinet_door_overlay": "Custom Overlay"}},
        is_active=True,
    )
    catalog_intent_session.add(registry)
    catalog_intent_session.commit()

    payload = {
        "source_document": {"collection_code": "GRACE"},
        "collection": {"display_name": "Grace Snow White", "color_label": "Snow White"},
        "specifications": {
            "style": {
                "door_style": "Shaker",
                "overlay": "Standard Overlay",
                "cabinet_frame_style": "Framed",
            }
        },
    }

    seed_collection_filter_profiles(
        catalog_intent_session,
        [payload],
        overwrite=False,
        dry_run=False,
    )
    catalog_intent_session.commit()
    catalog_intent_session.refresh(registry)

    assert registry.aliases["filter_attributes"]["cabinet_door_overlay"] == "Custom Overlay"
    assert registry.aliases["brochure_filter_attributes"]["cabinet_door_overlay"] == "Standard Overlay"
