"""Typed catalogs App Blocks resolve against. Feature-pack existence stays in Core."""

from collections.abc import Callable

from src.domain.feature_packs.contract import RESOLVABLE_PRIMITIVES

# Semantic pack ids used in manifests → catalog slugs actually seeded in Core.
FEATURE_PACK_ALIASES = {
    "forms.public_submission": "contact-capture",
    "contact.capture": "contact-capture",
    "crm.lead_lifecycle": "lead-lifecycle",
    "crm.lead": "lead-lifecycle",
    "workflow.case_lifecycle": "case-lifecycle",
    "case.lifecycle": "case-lifecycle",
    "workflow.approval": "approval-workflow",
    "workflow.assignment": "assignment-tracking",
    "assignment.tracking": "assignment-tracking",
    "task.management": "task-management",
    "task.tracking": "task-management",
    "activity.timeline": "activity-timeline",
    "activity.tracking": "activity-timeline",
    "notifications.in_app": "notifications-in-app",
    "notifications.tracking": "notifications-in-app",
    "files.attachments": "file-attachments",
    "file.attachments": "file-attachments",
    "document.record": "documents-lifecycle",
    "document.lifecycle": "documents-lifecycle",
    "booking.appointment": "appointment-booking",
    "payments.checkout": "payment-checkout",
    "payments.subscription": "saas-membership",
    "support.ticket": "support-inbox",
    "communication.messaging": "messaging",
    "directory.business": "business-directory",
    "social.feed": "social-feed",
    "marketplace.classifieds": "classifieds",
    "ai.assistant": "ai-knowledge-assistant",
    "admin.staff_management": "staff-admin-console",
    "client.portal": "client-portal",
    "requirements.fulfillment": "requirements-fulfillment",
    "record.conversations": "messaging",
    # content.ingest_webhook is the App Block id (content.ingest_webhook.v1.yaml),
    # not an alias -- it requires content-ingest directly by slug.
    "content.item": "content-ingest",
    "content.preset.testimonial": "content-presets",
    "content.preset.faq": "content-presets",
    "content.preset.service": "content-presets",
    "content.preset.team_member": "content-presets",
    "content.preset.location": "content-presets",
    "content.preset.pricing_tier": "content-presets",
    "content.preset.gallery_item": "content-presets",
    "content.preset.event": "content-presets",
}

DEFAULT_KNOWN_PACKS = frozenset(
    {
        "contact-capture",
        "lead-lifecycle",
        "customer-records",
        "case-lifecycle",
        "approval-workflow",
        "assignment-tracking",
        "task-management",
        "activity-timeline",
        "notifications-in-app",
        "file-attachments",
        "documents-lifecycle",
        "product-catalog",
        "order-management",
        "billing-management",
        "inventory-management",
        "membership-management",
        "renewal-operations",
        "support-inbox",
        "appointment-booking",
        "payment-checkout",
        "saas-membership",
        "subscription-operations",
        "education-learning",
        "billing-portal",
        "tax-calculation",
        "crm-pipeline",
        "client-portal",
        "ai-knowledge-assistant",
        "staff-admin-console",
        "business-directory",
        "connections-networking",
        "social-feed",
        "messaging",
        "requirements-fulfillment",
        "classifieds",
        "salesforce-order-sync",
        "reporting-dashboards",
        "organization-records",
        "quote-lifecycle",
        "comment-threads",
        "governance-groups",
        "customer-success-accounts",
        "contract-management",
        "workforce-directory",
        "calculator-mortgage",
        "content-ingest",
        "content-presets",
    }
)


def canonical_feature_pack(pack_id: str) -> str:
    return FEATURE_PACK_ALIASES.get(pack_id, pack_id)


def default_primitives() -> frozenset[str]:
    return RESOLVABLE_PRIMITIVES


def default_known_packs(loader: Callable[[], frozenset[str]] | None = None) -> frozenset[str]:
    if loader:
        return loader()
    return DEFAULT_KNOWN_PACKS


def pack_preferred_for_new_projects(slug: str) -> bool:
    from src.domain.feature_packs.normalize import load_pack_contract

    return load_pack_contract(slug, {}).planner.preferred_for_new_projects
