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,
)
from db.collection_landing_pages import collection_start_shopping_options
from db.models import MasterTaxonomyNode


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"


def test_collection_start_shopping_options_keeps_direct_hub_parent(catalog_intent_session):
    session = catalog_intent_session
    hub = MasterTaxonomyNode(
        parent_id=None,
        node_kind="hub",
        code="bathroom-vanities",
        name="Bathroom Vanities",
        path_slug="bathroom-vanities",
        is_active=True,
    )
    session.add(hub)
    session.flush()
    session.add(
        MasterTaxonomyNode(
            parent_id=hub.id,
            node_kind="category",
            code="base-cabinets",
            name="Bathroom Vanities / Base Cabinets",
            path_slug="bathroom-vanities/base-cabinets",
            is_active=True,
        )
    )
    session.flush()

    payload = collection_start_shopping_options(
        session,
        category_l1="Kitchen Cabinets",
        parent_path_slug="bathroom-vanities",
    )

    assert payload["hub_path_slug"] == "bathroom-vanities"
    assert payload["options"] == []


def test_collection_start_shopping_options_follow_hub_facet_defaults(catalog_intent_session):
    session = catalog_intent_session
    hub = MasterTaxonomyNode(
        parent_id=None,
        node_kind="hub",
        code="kitchen-cabinets",
        name="Kitchen Cabinets",
        path_slug="kitchen-cabinets",
        is_active=True,
    )
    session.add(hub)
    session.flush()
    session.add_all(
        [
            MasterTaxonomyNode(
                parent_id=hub.id,
                node_kind="category",
                code="wall-cabinets",
                name="Kitchen Cabinets / Wall Cabinets",
                path_slug="kitchen-cabinets/wall-cabinets",
                sort_order=20,
                is_active=True,
            ),
            MasterTaxonomyNode(
                parent_id=hub.id,
                node_kind="category",
                code="base-cabinets",
                name="Kitchen Cabinets / Base Cabinets",
                path_slug="kitchen-cabinets/base-cabinets",
                sort_order=10,
                is_active=True,
            ),
        ]
    )
    session.flush()

    payload = collection_start_shopping_options(
        session,
        category_l1="Kitchen Cabinets",
        parent_path_slug="kitchen-cabinets",
    )

    assert payload["hub_path_slug"] == "kitchen-cabinets"
    assert [item["path_slug"] for item in payload["options"][:2]] == [
        "kitchen-cabinets/base-cabinets",
        "kitchen-cabinets/wall-cabinets",
    ]
