"""P1-4: CRM + Tasks + Notifications compose without accidental contact capture."""
from src.domain.app_blocks.compiler import compile_install_plan
from src.domain.app_blocks.planner import ProductIntent, select_capabilities
from src.domain.projects.discovery_schemas import (
    Actor,
    AuthenticationSpec,
    DiscoverySpec,
    Entity,
    ProductDefinition,
    TenancySpec,
    Workflow,
)

PURPOSE = (
    "Staff manage customer records, a sales pipeline board, "
    "an internal task board, and an in-app notification inbox."
)
GOAL = "Let staff work customers, pipeline stages, tasks, and their notification inbox."


def _spec() -> DiscoverySpec:
    return DiscoverySpec(
        product=ProductDefinition(
            name="Staff operations",
            name_status="working",
            purpose=PURPOSE,
            first_build_goal=GOAL,
        ),
        archetypes=["internal_tool"],
        actors=[
            Actor(id="staff", name="Staff"),
            Actor(id="visitor", name="Website visitor"),
        ],
        workflows=[
            Workflow(id="review", name="Review customers", description="Staff update records"),
            Workflow(id="tasks", name="Track staff tasks", description="Staff move tasks"),
        ],
        entities=[
            Entity(id="customer", name="Customer", description="A customer record"),
            Entity(id="task", name="Staff task", description="An internal task"),
        ],
        tenancy=TenancySpec(model="single_workspace"),
        authentication=AuthenticationSpec(required=True),
    )


def test_cross_family_planner_selects_crm_tasks_notifications_without_contact():
    intent = ProductIntent(
        purpose=PURPOSE,
        first_build_goal=GOAL,
        actor_names=["Staff", "Website visitor"],
        workflow_names=["Review customers", "Track staff tasks"],
        entity_names=["Customer", "Staff task"],
        authentication_required=True,
        staff_review=True,
    )
    blocks = {block for _, block, _ in select_capabilities(intent)}
    assert {"crm.customer", "crm.pipeline", "task.management", "notifications.in_app"} <= blocks
    assert "forms.contact" not in blocks
    assert "crm.lead" not in blocks


def test_cross_family_install_plan_is_satisfiable_without_contact_pack():
    plan = compile_install_plan(_spec())
    ids = {block.id for block in plan.blocks}
    assert not plan.unsatisfiable
    assert {"crm.customer", "crm.pipeline", "task.management", "notifications.in_app"} <= ids
    assert {"admin.shell", "auth.sign_in"} <= ids
    assert "forms.contact" not in ids
    packs = set(plan.required_feature_packs)
    expected = {
        "customer-records",
        "lead-lifecycle",
        "task-management",
        "notifications-in-app",
    }
    assert expected <= packs
    assert "contact-capture" not in packs
    assert packs == expected
