import pytest

from db.master_taxonomy_delete import deactivate_taxonomy_node, deactivate_taxonomy_node_by_path
from db.models import (
    ChannelListingPath,
    ChannelListingPathTarget,
    CollectionLandingPage,
    ListingIntersectionRule,
    MasterTaxonomyChannelLink,
    MasterTaxonomyNode,
    ProductChannelTaxonomyAssignment,
    ProductListingPathAssignment,
)


def test_deactivate_taxonomy_node_cleans_local_publish_links(catalog_intent_session):
    session = catalog_intent_session
    hub = MasterTaxonomyNode(
        node_kind="hub",
        code="kitchen-cabinets",
        name="Kitchen Cabinets",
        path_slug="kitchen-cabinets",
    )
    node = MasterTaxonomyNode(
        parent_id=None,
        node_kind="collection",
        code="stone-gray",
        name="Stone Gray",
        path_slug="kitchen-cabinets/stone-gray",
    )
    session.add_all([hub, node])
    session.flush()
    node.parent_id = hub.id

    path = ChannelListingPath(
        path_slug=node.path_slug,
        path_kind="collection",
        title=node.name,
        parent_path_slug=hub.path_slug,
        hub_path_slug=hub.path_slug,
        is_active=True,
    )
    session.add(path)
    session.flush()
    session.add_all(
        [
            ChannelListingPathTarget(
                listing_path_id=path.id,
                channel_code="magento",
                connection_id=2,
                taxonomy_kind="collection",
                remote_id="77",
                is_active=True,
            ),
            ProductListingPathAssignment(
                master_sku="SKU-1",
                listing_path_id=path.id,
                assignment_status="active",
            ),
            ProductChannelTaxonomyAssignment(
                master_sku="SKU-1",
                channel_code="magento",
                connection_id=2,
                taxonomy_kind="collection",
                remote_id="77",
                assignment_status="active",
            ),
            MasterTaxonomyChannelLink(
                taxonomy_node_id=node.id,
                channel_code="magento",
                connection_id=2,
                taxonomy_kind="collection",
                remote_id="77",
                is_active=True,
            ),
            CollectionLandingPage(
                path_slug=node.path_slug,
                page_type="collection",
                category_l1="Kitchen Cabinets",
                collection="Stone Gray",
                title="Stone Gray",
                is_active=True,
            ),
            ListingIntersectionRule(
                base_path_slug=hub.path_slug,
                group_path_slug=node.path_slug,
                group_kind="collection",
                facet_attribute="cabinet_type",
                facet_label="Wall Cabinets",
                facet_value="wallcabinets",
                facet_slug="wall-cabinets",
                seo_path_slug=f"{node.path_slug}/wall-cabinets",
                title="Wall Cabinets - Stone Gray",
                is_active=True,
            ),
        ]
    )
    session.flush()

    result = deactivate_taxonomy_node(session, node.id)

    assert result["deactivated_nodes"] == 1
    assert result["deactivated_listing_paths"] == 1
    assert result["deactivated_listing_path_targets"] == 1
    assert result["deactivated_listing_assignments"] == 1
    assert result["deactivated_channel_links"] == 1
    assert result["deactivated_channel_assignments"] == 1
    assert result["deactivated_collection_landing_pages"] == 1
    assert result["deactivated_intersections"] == 1
    assert result["remote_delete_performed"] is False

    assert session.get(MasterTaxonomyNode, node.id).is_active is False
    assert session.get(ChannelListingPath, path.id).is_active is False


def test_deactivate_taxonomy_node_blocks_active_children_without_cascade(catalog_intent_session):
    session = catalog_intent_session
    parent = MasterTaxonomyNode(
        node_kind="category",
        code="parent",
        name="Parent",
        path_slug="kitchen-cabinets/parent",
    )
    session.add(parent)
    session.flush()
    child = MasterTaxonomyNode(
        parent_id=parent.id,
        node_kind="collection",
        code="child",
        name="Child",
        path_slug="kitchen-cabinets/parent/child",
    )
    session.add(child)
    session.flush()

    with pytest.raises(ValueError, match="active children"):
        deactivate_taxonomy_node(session, parent.id)


def test_deactivate_taxonomy_node_by_path(catalog_intent_session):
    session = catalog_intent_session
    node = MasterTaxonomyNode(
        node_kind="collection",
        code="pearl-white",
        name="Pearl White",
        path_slug="kitchen-cabinets/pearl-white",
    )
    session.add(node)
    session.flush()

    result = deactivate_taxonomy_node_by_path(session, "kitchen-cabinets/pearl-white")

    assert result["node_ids"] == [node.id]
    assert session.get(MasterTaxonomyNode, node.id).is_active is False
