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


CONNECTOR = ConnectorDef(
    key="mailgun",
    name="Mailgun",
    category="email",
    auth_type="api_key",
    version="01.00.00",
    icon="mailgun",
    image_url=None,  # set to your CDN URL
    description="Transactional email delivery, scheduled delivery, recipient-variable batch sending, stored message retrieval, resend, and domain queue controls.",
    docs_url="https://documentation.mailgun.com/docs/mailgun/api-reference/send/mailgun/messages",
    config_fields=("domain", "region", "from_email", "from_name", "storage_api_base_url",),
    secret_fields=("api_key",),
    config_field_defs=(
        ConnectorFieldDef(
            key="domain",
            label="Sending domain",
            description="A domain verified in your Mailgun account.",
            placeholder="mg.yourdomain.com",
            required=True,
        ),
        ConnectorFieldDef(
            key="region",
            label="Region",
            description="Mailgun API region. Use 'eu' if your domain was added under the EU region.",
            placeholder="us",
            default="us",
        ),
        ConnectorFieldDef(
            key="from_email",
            label="From email",
            placeholder="noreply@yourdomain.com",
            required=True,
        ),
        ConnectorFieldDef(
            key="from_name",
            label="From name",
            placeholder="Acme Inc.",
        ),
        ConnectorFieldDef(
            key="storage_api_base_url",
            label="Storage API base URL",
            description="Optional. Only needed for stored-message retrieval/resend actions; leave blank otherwise.",
            input_type="url",
        ),
    ),
    secret_field_defs=(
        ConnectorFieldDef(
            key="api_key",
            label="API key",
            required=True,
            input_type="password",
        ),
    ),
    actions=(
        ConnectorActionDef(
            key="send_email",
            slug="mailgun.send_email",
            name="Send email",
            description="Send a Mailgun message immediately or schedule it with scheduled_at.",
            input_schema={
                "type": "object",
                "properties": {
                    "domain": {"type": "string"},
                    "from": {"type": "string"},
                    "to": {"type": ["string", "array"]},
                    "cc": {"type": ["string", "array"]},
                    "bcc": {"type": ["string", "array"]},
                    "reply_to": {"type": ["string", "array"]},
                    "subject": {"type": "string"},
                    "text": {"type": "string"},
                    "html": {"type": "string"},
                    "template": {"type": "string"},
                    "template_variables": {"type": "object"},
                    "headers": {"type": "object"},
                    "variables": {"type": "object"},
                    "tags": {"type": "array"},
                    "scheduled_at": {"type": "string"},
                    "test_mode": {"type": "boolean"},
                    "tracking": {"type": "boolean"},
                    "tracking_clicks": {"type": ["string", "boolean"]},
                    "tracking_opens": {"type": "boolean"},
                },
                "required": ["to", "subject"],
                "additionalProperties": True,
            },
            required_capabilities=("integrations.execute",),
            implements=("email.send",),
        ),
        ConnectorActionDef(
            key="send_batch_email",
            slug="mailgun.send_batch_email",
            name="Send batch email",
            description="Send one Mailgun message to up to 1,000 recipients using recipient-variables for per-recipient personalization.",
            input_schema={
                "type": "object",
                "properties": {
                    "domain": {"type": "string"},
                    "from": {"type": "string"},
                    "to": {"type": "array"},
                    "cc": {"type": ["string", "array"]},
                    "bcc": {"type": ["string", "array"]},
                    "reply_to": {"type": ["string", "array"]},
                    "subject": {"type": "string"},
                    "text": {"type": "string"},
                    "html": {"type": "string"},
                    "template": {"type": "string"},
                    "template_variables": {"type": "object"},
                    "recipient_variables": {"type": "object"},
                    "headers": {"type": "object"},
                    "variables": {"type": "object"},
                    "tags": {"type": "array"},
                    "scheduled_at": {"type": "string"},
                    "test_mode": {"type": "boolean"},
                },
                "required": ["to", "subject", "recipient_variables"],
                "additionalProperties": True,
            },
            required_capabilities=("integrations.execute",),
        ),
        ConnectorActionDef(
            key="retrieve_email",
            slug="mailgun.retrieve_email",
            name="Retrieve stored email",
            description="Retrieve a stored Mailgun email by storage_key from Mailgun events.",
            input_schema={
                "type": "object",
                "properties": {"domain": {"type": "string"}, "storage_key": {"type": "string"}},
                "required": ["storage_key"],
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
        ),
        ConnectorActionDef(
            key="resend_email",
            slug="mailgun.resend_email",
            name="Resend stored email",
            description="Resend a stored Mailgun email by storage_key.",
            input_schema={
                "type": "object",
                "properties": {
                    "domain": {"type": "string"},
                    "storage_key": {"type": "string"},
                    "to": {"type": ["string", "array"]},
                },
                "required": ["storage_key"],
                "additionalProperties": True,
            },
            required_capabilities=("integrations.execute",),
        ),
        ConnectorActionDef(
            key="get_sending_queue",
            slug="mailgun.get_sending_queue",
            name="Get sending queue",
            description="Retrieve default and scheduled queue status for a Mailgun domain.",
            input_schema={
                "type": "object",
                "properties": {"domain": {"type": "string"}},
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
        ),
        ConnectorActionDef(
            key="cancel_domain_queue",
            slug="mailgun.cancel_domain_queue",
            name="Cancel domain queue",
            description="Delete all scheduled and undelivered mail from a Mailgun domain queue. Mailgun cancellation is domain queue-level, not individual email-level.",
            input_schema={
                "type": "object",
                "properties": {
                    "domain": {"type": "string"},
                    "storage_api_base_url": {"type": "string"},
                },
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
        ),
    ),
    triggers=(
        ConnectorTriggerDef(
            key="email_accepted",
            event_type="email.accepted",
            name="Email accepted",
        ),
        ConnectorTriggerDef(
            key="email_delivered",
            event_type="email.delivered",
            name="Email delivered",
        ),
        ConnectorTriggerDef(
            key="email_failed",
            event_type="email.failed",
            name="Email failed",
        ),
        ConnectorTriggerDef(
            key="email_opened",
            event_type="email.opened",
            name="Email opened",
        ),
        ConnectorTriggerDef(
            key="email_clicked",
            event_type="email.clicked",
            name="Email clicked",
        ),
        ConnectorTriggerDef(
            key="email_complained",
            event_type="email.complained",
            name="Email complained",
        ),
        ConnectorTriggerDef(
            key="email_unsubscribed",
            event_type="email.unsubscribed",
            name="Email unsubscribed",
        ),
    ),
    changelog=(
        ConnectorChangelogEntry(
            version="01.00.00",
            date="2026-04-26",
            notes="Initial Mailgun connector with send, scheduled delivery, recipient-variable batch send, stored message retrieve/resend, sending queue status, domain queue cancellation, and email lifecycle webhooks.",
        ),
    ),
)
