"""Operate console options. Core admits them; Dash does not keep a private list."""

from __future__ import annotations

from src.integrations.platform_ports import allowed_request_types, enabled_capabilities

# Submit ids for payments are the Cloud queue names. Capability paths stay
# on the catalog row so codegen and the invoke table still share one id.
_PAYMENT_SUBMIT_IDS = {
    "payments.checkout.create": "payment.checkout.create",
    "payments.order.capture": "payment.capture",
    "payments.refund.create": "payment.refund",
    "payments.catalog.upsert": "payment.catalog.upsert",
}

_ROWS: tuple[dict, ...] = (
    {
        "label": "Automation — trigger workflow",
        "capability": "automation.workflow.trigger",
        "gate": "integrations",
        "example": {"workflow": "example", "input": {}},
    },
    {
        "label": "CRM — search contacts",
        "capability": "crm.contact.search",
        "gate": "integrations",
        "example": {"query": ""},
    },
    {
        "label": "CRM — search companies",
        "capability": "crm.company.search",
        "gate": "integrations",
        "example": {"query": ""},
    },
    {
        "label": "CRM — search deals",
        "capability": "crm.deal.search",
        "gate": "integrations",
        "example": {"query": ""},
    },
    {
        "label": "Payments — create checkout",
        "capability": "payments.checkout.create",
        "gate": "payment",
        "example": {
            "success_url": "https://example.com/success",
            "cancel_url": "https://example.com/cancel",
            "items": [],
        },
    },
    {
        "label": "Payments — capture order",
        "capability": "payments.order.capture",
        "gate": "payment",
        "example": {"order_id": ""},
    },
    {
        "label": "Payments — create refund",
        "capability": "payments.refund.create",
        "gate": "payment",
        "example": {"order_id": "", "amount": 0},
    },
)


def admitted_operate_invocations(settings: dict | None = None) -> list[dict]:
    """Rows the dashboard may submit for this project's current entitlements."""
    settings = settings or {}
    if settings.get("cloud_enabled") is False:
        return []
    caps = enabled_capabilities(settings)
    allowed = allowed_request_types(settings)
    integrations = "integrations.execute" in caps
    rows: list[dict] = []
    for row in _ROWS:
        if row["gate"] == "integrations" and not integrations:
            continue
        submit_id = _PAYMENT_SUBMIT_IDS.get(row["capability"], row["capability"])
        if row["gate"] == "payment" and submit_id not in allowed:
            continue
        rows.append(
            {
                "id": submit_id,
                "label": row["label"],
                "capability": row["capability"],
                "request_type": submit_id if row["gate"] == "payment" else "integration.execute",
                "example": row["example"],
            }
        )
    return rows
