"""P1-1: default Website visitor actor must not imply forms.contact."""
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,
)

CONTACT = "forms.contact"
QUOTE = "forms.quote_request"


def _blocks(intent: ProductIntent) -> set[str]:
    return {block for _, block, _ in select_capabilities(intent)}


def _spec(*, purpose: str, goal: str, extra_actors=(), workflows=(), entities=(), auth=False):
    actors = [
        Actor(id="visitor", name="Website visitor"),
        Actor(id="staff", name="Staff"),
        *extra_actors,
    ]
    product = ProductDefinition(
        name="Fixture",
        name_status="working",
        purpose=purpose,
        first_build_goal=goal,
    )
    return DiscoverySpec(
        product=product,
        archetypes=["internal_tool"],
        actors=actors,
        workflows=list(workflows),
        entities=list(entities),
        tenancy=TenancySpec(model="single_workspace"),
        authentication=AuthenticationSpec(required=auth),
    )


def test_default_website_visitor_actor_does_not_select_contact():
    intent = ProductIntent(
        purpose="Staff manage customer records",
        first_build_goal="Keep a customer list",
        actor_names=["Website visitor", "Staff"],
    )
    assert CONTACT not in _blocks(intent)


def test_explicit_contact_intent_still_selects_contact_with_default_actors():
    intent = ProductIntent(
        purpose="Allow prospective customers to contact the agency.",
        first_build_goal="Capture website enquiries and let staff review them.",
        actor_names=["Website visitor", "Staff"],
        workflow_names=["Submit enquiry"],
        entity_names=["Enquiry"],
        archetypes=["marketing_site"],
    )
    assert CONTACT in _blocks(intent)


def test_explicit_quote_intent_still_selects_quote_not_contact():
    intent = ProductIntent(
        purpose="Let homeowners request a roofing quote.",
        first_build_goal="Capture quote requests",
        actor_names=["Website visitor", "Staff"],
    )
    blocks = _blocks(intent)
    assert QUOTE in blocks
    assert CONTACT not in blocks


def test_unrelated_families_do_not_gain_contact_from_website_visitor():
    visitor = ["Website visitor", "Staff"]
    purposes = [
        "Staff manage customer records",
        "Staff manage a sales pipeline board",
        "Book an appointment with bookable slots",
        "Staff manage a product catalog",
        "We need an internal task board for staff",
        "The product requires signed-in members",
        "We need an in-app notification inbox",
    ]
    for purpose in purposes:
        intent = ProductIntent(purpose=purpose, actor_names=visitor)
        assert CONTACT not in _blocks(intent), purpose


def test_website_in_purpose_still_selects_contact():
    intent = ProductIntent(purpose="Capture website enquiries from the public")
    assert CONTACT in _blocks(intent)


def test_compiled_crm_spec_with_default_actors_omits_contact_pack():
    spec = _spec(
        purpose="Staff manage customer records and a sales pipeline board",
        goal="Keep customers and pipeline stages",
        workflows=[
            Workflow(id="review", name="Review customers", description="Staff update records"),
        ],
        entities=[Entity(id="customer", name="Customer", description="A customer record")],
        auth=True,
    )
    plan = compile_install_plan(spec)
    ids = {block.id for block in plan.blocks}
    assert "crm.customer" in ids
    assert CONTACT not in ids
    assert "contact-capture" not in plan.required_feature_packs
