"""Remaining families through #26: reuse existing catalog, no invented primitives."""
from src.domain.app_blocks.canonical import CANONICAL_BLOCK_IDS, feature_pack_only_aliases
from src.domain.app_blocks.catalogs import FEATURE_PACK_ALIASES, pack_preferred_for_new_projects
from src.domain.app_blocks.planner import RULES, 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_planner_only_emits_admitted_canonical_blocks():
    assert {rule.block_id for rule in RULES} <= CANONICAL_BLOCK_IDS
    for purpose in (
        "Member portal with my account and subscriptions",
        "Analytics dashboard and full-text search",
        "Organization permissions and custom ACL",
        "Support ticket inbox and Zendesk clone",
        "AI agent swarm with custom tools",
        "Enterprise SSO and SCIM",
        "Build a connector SDK",
    ):
        blocks = _selected(purpose)
        assert blocks <= CANONICAL_BLOCK_IDS


def test_membership_reuses_saas_membership_and_auth_blocks():
    assert "auth.sign_in" in CANONICAL_BLOCK_IDS
    assert "auth.profile" in CANONICAL_BLOCK_IDS
    assert "membership.member" in CANONICAL_BLOCK_IDS
    assert "membership.portal" not in CANONICAL_BLOCK_IDS
    assert FEATURE_PACK_ALIASES["payments.subscription"] == "saas-membership"
    assert pack_preferred_for_new_projects("saas-membership")
    assert not pack_preferred_for_new_projects("client-portal")
    assert overlay_for("saas-membership")["entities"][0].entity_key == "app_subscription"
    assert overlay_for("saas-membership")["security"].rls_required is True
    assert overlay_for("membership-management")["entities"][0].entity_key == "membership_record"
    portal = _selected("Member portal with my account and subscriptions")
    assert "membership.member" not in portal


def test_reporting_search_orgs_have_no_product_blocks():
    for prefix in ("reporting.", "search.", "orgs.", "organizations.", "permissions."):
        assert not any(block_id.startswith(prefix) for block_id in CANONICAL_BLOCK_IDS)


def test_support_is_not_rebound_to_cases():
    # GR-05 (Phase 13, explicitly authorized) admitted support.ticket as its
    # own App Block over the pre-existing support-inbox Feature Pack -- it is
    # no longer feature-pack-only, it is now dual-namespace (like crm.lead
    # and booking.appointment: a valid Feature Pack alias key that is also a
    # real App Block id). See docs/audits/GR05_EXIT_REVIEW.md. The core guard
    # this test exists for -- support tickets never silently resolve to
    # case.management -- is unchanged and, if anything, more strongly proven
    # now that support.ticket has its own dedicated block.
    assert FEATURE_PACK_ALIASES["support.ticket"] == "support-inbox"
    assert "support.ticket" not in feature_pack_only_aliases()
    assert "support.ticket" in CANONICAL_BLOCK_IDS
    assert "support.inbox" not in CANONICAL_BLOCK_IDS
    assert "case.management" in CANONICAL_BLOCK_IDS
    case_keys = {entity.entity_key for entity in overlay_for("case-lifecycle")["entities"]}
    support_keys = {entity.entity_key for entity in overlay_for("support-inbox")["entities"]}
    assert case_keys == {"case_record"}
    assert support_keys == {"support_ticket", "support_message"}
    tickets = select_capabilities(ProductIntent(purpose="A support ticket inbox for customers"))
    assert any(block_id == "support.ticket" for _cap, block_id, _reason in tickets)
    assert not any(block_id == "case.management" for _cap, block_id, _reason in tickets)


def test_ai_connectors_enterprise_reuse_existing_surfaces():
    assert overlay_for("ai-knowledge-assistant")["provides"][0].id == "ai.assistant"
    assert not any(block_id.startswith("ai.") for block_id in CANONICAL_BLOCK_IDS)
    assert not any(block_id.startswith("connectors.") for block_id in CANONICAL_BLOCK_IDS)
    assert not any(block_id.startswith("enterprise.") for block_id in CANONICAL_BLOCK_IDS)
    assert pack_preferred_for_new_projects("ai-knowledge-assistant")
