from src.domain.connectors.catalog._base import (
    ConnectorActionDef,
    ConnectorChangelogEntry,
    ConnectorDef,
    ConnectorFieldDef,
)


CONNECTOR = ConnectorDef(
    key="n8n",
    name="n8n",
    category="automation",
    auth_type="api_key",
    version="01.01.00",
    icon="n8n",
    image_url=None,  # set to your CDN URL
    description="Trigger an n8n workflow from your app via its Webhook node.",
    docs_url="https://docs.n8n.io/integrations/core-nodes/n8n-nodes-base.webhook/",
    config_fields=("webhook_url", "workflow",),
    secret_fields=("webhook_auth_header_value",),
    config_field_defs=(
        ConnectorFieldDef(
            key="webhook_url",
            label="Production webhook URL",
            description="Production URL from the Webhook node. Not called when you save.",
            placeholder="https://your-instance.app.n8n.cloud/webhook/...",
            required=True,
            input_type="url",
        ),
        ConnectorFieldDef(
            key="workflow",
            label="Logical workflow name",
            description=(
                "Optional P2HS alias (e.g. lead-intake). Not an n8n workflow ID. "
                "Required to bind this connection to automation.workflow.trigger."
            ),
            placeholder="lead-intake",
        ),
    ),
    secret_field_defs=(
        ConnectorFieldDef(
            key="webhook_auth_header_value",
            label="Webhook auth header value",
            description="Optional Authorization value when the Webhook node uses Header Auth.",
            input_type="password",
        ),
    ),
    actions=(
        ConnectorActionDef(
            key="trigger_workflow",
            slug="n8n.trigger_workflow",
            name="Trigger workflow",
            description="POST a JSON payload to the configured n8n webhook to start that workflow.",
            input_schema={
                "type": "object",
                "properties": {"payload": {"type": "object"}},
                "required": ["payload"],
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
            implements=("automation.workflow.trigger",),
        ),
    ),
    changelog=(
        ConnectorChangelogEntry(
            version="01.00.00",
            date="2026-09-06",
            notes="Initial release. Webhook-based workflow triggering, with optional REST API key for direct workflow execution.",
        ),
        ConnectorChangelogEntry(
            version="01.01.00",
            date="2026-09-12",
            notes=(
                "Phase 1C: implements automation.workflow.trigger. "
                "Logical workflow alias is connection-side; webhook URL stays provider addressing."
            ),
        ),
    ),
)
