import pytest

from db.product_channel_taxonomy import _category_ids_from_rule, _category_paths_from_rule
from shopify.taxonomy_sync import _normalize, _score_match


def test_category_ids_from_rule_accepts_list_and_scalar():
    assert _category_ids_from_rule({"category_ids": [12, "34"]}) == [12, 34]
    assert _category_ids_from_rule({"category_id": "99"}) == [99]


def test_category_paths_from_rule_accepts_pipe_delimited():
    assert _category_paths_from_rule({"category_paths": "A|B"}) == ["A", "B"]


def test_shopify_taxonomy_match_scores_contained_needles():
    needles = [_normalize("Kitchen Cabinets")]
    haystack = _normalize("Home & Garden > Furniture > Kitchen Cabinets")
    assert _score_match(needles, haystack) > 0


@pytest.mark.parametrize(
    "taxonomy_kind,replace_default",
    [
        ("collection", False),
        ("product_category", False),
        ("category", False),
    ],
)
def test_bulk_assign_requires_remote_ids(taxonomy_kind, replace_default):
    from db.product_channel_taxonomy import bulk_assign_sku_taxonomy

    class FakeSession:
        pass

    with pytest.raises(ValueError, match="remote_ids"):
        bulk_assign_sku_taxonomy(
            FakeSession(),
            channel_code="shopify",
            remote_ids=[],
            taxonomy_kind=taxonomy_kind,
            replace_existing=replace_default,
        )


def test_create_shopify_collection_requires_title():
    from shopify.collection_sync import create_shopify_collection

    class FakeSession:
        pass

    class FakeClient:
        pass

    with pytest.raises(ValueError, match="title"):
        create_shopify_collection(
            FakeSession(),
            FakeClient(),
            shop_code="demo",
            connection_id=1,
            title="",
        )
