"""11H2 checkout order reuse, plus Architecture Lane application Orders.

payment-checkout.order remains job-keyed Stripe checkout state.
orders.order persists order_record through data.entity and is not that store.
"""
from src.domain.app_blocks.canonical import CANONICAL_BLOCK_IDS, feature_pack_only_aliases
from src.domain.app_blocks.catalogs import FEATURE_PACK_ALIASES
from src.domain.app_blocks.planner import ProductIntent, select_capabilities
from src.domain.feature_packs.catalog_v2 import overlay_for


def _selected(purpose: str) -> set[str]:
    intent = ProductIntent(purpose=purpose)
    return {block_id for _cap, block_id, _reason in select_capabilities(intent)}


def test_checkout_alias_is_dual_namespace():
    assert FEATURE_PACK_ALIASES["payments.checkout"] == "payment-checkout"
    assert "payments.checkout" not in feature_pack_only_aliases()
    assert "payments.checkout" in CANONICAL_BLOCK_IDS
    assert "orders.order" in CANONICAL_BLOCK_IDS
    assert "orders.order" not in feature_pack_only_aliases()


def test_canonical_checkout_order_entity_stays_on_checkout_pack():
    checkout = overlay_for("payment-checkout")
    keys = {entity.entity_key for entity in checkout["entities"]}
    assert keys == {"product", "price", "order"}
    order = next(entity for entity in checkout["entities"] if entity.entity_key == "order")
    assert order.schema_mode == "protected"
    application = overlay_for("order-management")
    assert application["entities"][0].entity_key == "order_record"
    assert overlay_for("product-catalog")["entities"][0].entity_key == "catalog_item"
    catalog_keys = {entity.entity_key for entity in overlay_for("product-catalog")["entities"]}
    assert "order" not in catalog_keys
    assert "order_record" not in catalog_keys
    sync = overlay_for("salesforce-order-sync")
    assert [cap.id for cap in sync["provides"]] == ["crm.external_order_sync"]
    assert not sync.get("entities")


def test_planner_selects_application_orders_without_checkout_or_catalog():
    blocks = _selected("Show customer order history after Stripe checkout")
    assert "orders.order" in blocks
    assert "catalog.product" not in blocks
    assert not any(block.startswith("payments.") for block in blocks)


def test_planner_does_not_bind_vendor_or_billing_copy_to_orders():
    for purpose in (
        "Shopify orders and Salesforce order sync",
        "Paid invoices and refunds",
    ):
        blocks = _selected(purpose)
        assert "orders.order" not in blocks
        assert "catalog.product" not in blocks
        assert not any(block_id.startswith("orders.") and block_id != "orders.order" for block_id in blocks)
