from db.collection_channel_placement import (
    build_channel_placement,
    default_parent_path_slug,
    effective_parent_path_slug,
    magento_breadcrumb,
    shopify_collection_handle,
    shopify_collection_title,
)


def test_default_parent_is_hub():
    assert default_parent_path_slug("Kitchen Cabinets") == "kitchen-cabinets"


def test_effective_parent_prefers_explicit():
    assert (
        effective_parent_path_slug(
            category_l1="Kitchen Cabinets",
            parent_path_slug="kitchen-cabinets/base-cabinets",
        )
        == "kitchen-cabinets/base-cabinets"
    )


def test_magento_breadcrumb_direct_collection_child():
    assert magento_breadcrumb(
        category_l1="Kitchen Cabinets",
        collection="Anna Stone Gray",
        parent_path_slug="kitchen-cabinets",
    ) == ["Kitchen Cabinets", "Anna Stone Gray"]


def test_magento_breadcrumb_nested_under_subcategory():
    assert magento_breadcrumb(
        category_l1="Kitchen Cabinets",
        collection="Anna Stone Gray",
        parent_path_slug="kitchen-cabinets/base-cabinets",
    ) == ["Kitchen Cabinets", "Base Cabinets", "Anna Stone Gray"]


def test_shopify_flat_title_and_handle():
    assert shopify_collection_title("Anna Snow White", "kitchen-cabinets/anna-snow-white") == "Anna Snow White"
    assert shopify_collection_handle("Anna Snow White") == "anna-snow-white"


def test_channel_placement_magento_hierarchical():
    placement = build_channel_placement(
        channel_code="magento",
        path_slug="kitchen-cabinets/anna-stone-gray",
        category_l1="Kitchen Cabinets",
        collection="Anna Stone Gray",
    )
    assert placement["structure"] == "hierarchical"
    assert placement["supports_hierarchy"] is True
    assert placement["master_parent_path_slug"] == "kitchen-cabinets"
    assert placement["breadcrumb"] == ["Kitchen Cabinets", "Anna Stone Gray"]


def test_channel_placement_shopify_flat():
    placement = build_channel_placement(
        channel_code="shopify",
        path_slug="kitchen-cabinets/anna-stone-gray",
        category_l1="Kitchen Cabinets",
        collection="Anna Stone Gray",
    )
    assert placement["structure"] == "flat"
    assert placement["supports_hierarchy"] is False
    assert placement["master_parent_path_slug"] is None
    assert placement["remote_title"] == "Anna Stone Gray"
    assert placement["remote_handle"] == "anna-stone-gray"
    assert placement["breadcrumb"] == ["Anna Stone Gray"]


def test_magento_breadcrumb_strips_duplicate_hub_prefix_from_collection():
    placement = build_channel_placement(
        channel_code="magento",
        path_slug="kitchen-cabinets/quest-metro-mist",
        category_l1="Kitchen Cabinets",
        collection="Kitchen Cabinets / Quest Metro Mist",
    )

    assert placement["breadcrumb"] == ["Kitchen Cabinets", "Quest Metro Mist"]


def test_shopify_collection_title_strips_duplicate_hub_prefix():
    assert shopify_collection_title("Kitchen Cabinets / Quest Metro Mist") == "Quest Metro Mist"
