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

CONNECTOR = ConnectorDef(
    key="paypal",
    name="PayPal",
    category="payments",
    auth_type="api_key",
    version="01.02.00",
    icon="paypal",
    image_url=None,  # set to your CDN URL
    description=(
        "PayPal Checkout orders, authorize/capture, refunds, "
        "and order lookup for generated storefronts."
    ),
    docs_url="https://developer.paypal.com/docs/api/orders/v2/",
    config_fields=("environment",),
    secret_fields=("client_id", "client_secret",),
    config_field_defs=(
        ConnectorFieldDef(
            key="environment",
            label="Environment",
            description="live or sandbox. Defaults to live.",
            placeholder="live",
        ),
    ),
    secret_field_defs=(
        ConnectorFieldDef(
            key="client_id",
            label="Client ID",
            description="PayPal REST app client ID.",
            required=True,
        ),
        ConnectorFieldDef(
            key="client_secret",
            label="Client secret",
            description="PayPal REST app secret.",
            required=True,
            input_type="password",
        ),
    ),
    actions=(
        ConnectorActionDef(
            key="create_order",
            slug="paypal.create_order",
            name="Create order",
            description="Create a PayPal Checkout order and return the approve link.",
            input_schema={
                "type": "object",
                "properties": {
                    "amount": {"type": "string"},
                    "currency": {"type": "string"},
                    "intent": {"type": "string"},
                    "return_url": {"type": "string"},
                    "cancel_url": {"type": "string"},
                    "reference_id": {"type": "string"},
                    "custom_id": {"type": "string"},
                    "description": {"type": "string"},
                    "metadata": {"type": "object"},
                },
                "required": ["amount"],
                "additionalProperties": True,
            },
            required_capabilities=("integrations.execute",),
            implements=("payments.checkout.create",),
        ),
        ConnectorActionDef(
            key="capture_order",
            slug="paypal.capture_order",
            name="Capture order",
            description="Capture an approved PayPal CAPTURE-intent order.",
            input_schema={
                "type": "object",
                "properties": {"order_id": {"type": "string"}},
                "required": ["order_id"],
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
            implements=("payments.order.capture",),
        ),
        ConnectorActionDef(
            key="authorize_order",
            slug="paypal.authorize_order",
            name="Authorize order",
            description="Authorize an approved PayPal AUTHORIZE-intent order (hold funds).",
            input_schema={
                "type": "object",
                "properties": {"order_id": {"type": "string"}},
                "required": ["order_id"],
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
            implements=("payments.order.capture",),
        ),
        ConnectorActionDef(
            key="capture_authorization",
            slug="paypal.capture_authorization",
            name="Capture authorization",
            description="Capture a previously authorized PayPal payment.",
            input_schema={
                "type": "object",
                "properties": {
                    "authorization_id": {"type": "string"},
                    "amount": {"type": "string"},
                    "currency": {"type": "string"},
                },
                "required": ["authorization_id"],
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
            implements=("payments.order.capture",),
        ),
        ConnectorActionDef(
            key="get_order",
            slug="paypal.get_order",
            name="Get order",
            description="Retrieve a PayPal order by id.",
            input_schema={
                "type": "object",
                "properties": {"order_id": {"type": "string"}},
                "required": ["order_id"],
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
        ),
        ConnectorActionDef(
            key="refund_capture",
            slug="paypal.refund_capture",
            name="Refund capture",
            description="Refund a captured PayPal payment.",
            input_schema={
                "type": "object",
                "properties": {
                    "capture_id": {"type": "string"},
                    "amount": {"type": "string"},
                    "currency": {"type": "string"},
                    "note": {"type": "string"},
                },
                "required": ["capture_id"],
                "additionalProperties": False,
            },
            required_capabilities=("integrations.execute",),
            implements=("payments.refund.create",),
        ),
    ),
    triggers=(
        ConnectorTriggerDef(
            key="checkout_order_approved",
            event_type="paypal.checkout.order.approved",
            name="Checkout order approved",
        ),
        ConnectorTriggerDef(
            key="payment_authorization_created",
            event_type="paypal.payment.authorization.created",
            name="Payment authorization created",
        ),
        ConnectorTriggerDef(
            key="payment_authorization_voided",
            event_type="paypal.payment.authorization.voided",
            name="Payment authorization voided",
        ),
        ConnectorTriggerDef(
            key="payment_capture_completed",
            event_type="paypal.payment.capture.completed",
            name="Payment capture completed",
        ),
        ConnectorTriggerDef(
            key="payment_capture_refunded",
            event_type="paypal.payment.capture.refunded",
            name="Payment capture refunded",
        ),
    ),
)
