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


CONNECTOR = ConnectorDef(
    key="microsoft_ads",
    name="Microsoft Ads",
    category="analytics",
    auth_type="oauth",
    version="01.01.00",
    client_side=True,
    icon="microsoft_ads",
    image_url=None,  # set to your CDN URL
    description="Microsoft/Bing UET tag for frontend conversion tracking and the Microsoft Advertising CampaignManagement OfflineConversions/Apply API for server-side uploads.",
    docs_url="https://learn.microsoft.com/en-us/advertising/campaign-management-service/applyofflineconversions",
    config_fields=("tag_id", "customer_account_id", "customer_id", "client_id",),
    secret_fields=("access_token", "developer_token", "refresh_token", "client_secret",),
    config_field_defs=(
        ConnectorFieldDef(
            key="tag_id",
            label="UET tag ID",
            description="Used by the frontend UET tag snippet for conversion tracking.",
            required=True,
        ),
        ConnectorFieldDef(
            key="customer_account_id",
            label="Account ID",
            description="The Microsoft Advertising account ID to upload conversions to. Sent as the CustomerAccountId header.",
            required=True,
        ),
        ConnectorFieldDef(
            key="customer_id",
            label="Customer (manager account) ID",
            description="The Microsoft Advertising manager/customer ID that owns the account above. Distinct from the account ID — required by the Offline Conversions API as the CustomerId header.",
            required=True,
        ),
        ConnectorFieldDef(
            key="client_id",
            label="OAuth Client (Application) ID",
            description="Client ID of the Azure AD app used to generate the refresh token. Required, with the client secret, to keep this connection working beyond the initial access token's ~1 hour lifetime.",
            required=True,
        ),
    ),
    secret_field_defs=(
        ConnectorFieldDef(
            key="access_token",
            label="Access token",
            description="Optional. A short-lived access token (expires in ~1 hour). Provide refresh_token, client_id, and client_secret so the connection keeps working after it expires.",
            input_type="password",
        ),
        ConnectorFieldDef(
            key="developer_token",
            label="Developer token",
            description="Developer token issued for your Microsoft Advertising account. Required for every API call.",
            required=True,
            input_type="password",
        ),
        ConnectorFieldDef(
            key="refresh_token",
            label="Refresh token",
            description="OAuth refresh token from the same Azure AD app as client_id. Required to keep this connection working long-term.",
            required=True,
            input_type="password",
        ),
        ConnectorFieldDef(
            key="client_secret",
            label="OAuth Client secret",
            description="Client secret of the Azure AD app used to generate the refresh token.",
            required=True,
            input_type="password",
        ),
    ),
    actions=(
        ConnectorActionDef(
            key="upload_conversion",
            slug="microsoft_ads.upload_conversion",
            name="Upload conversion",
            description="Upload an offline or enhanced conversion to Microsoft Advertising.",
            input_schema={
                "type": "object",
                "properties": {
                    "conversion_name": {"type": "string"},
                    "conversion_time": {"type": "string"},
                    "conversion_value": {"type": "number"},
                    "conversion_currency_code": {"type": "string"},
                    "microsoft_click_id": {"type": "string"},
                    "hashed_email": {"type": "string"},
                    "hashed_phone": {"type": "string"},
                    "external_attribution_credit": {"type": "number"},
                    "external_attribution_model": {"type": "string"},
                },
                "required": ["conversion_name", "conversion_time"],
                "additionalProperties": True,
            },
            required_capabilities=("integrations.execute",),
        ),
        ConnectorActionDef(
            key="track_event",
            slug="microsoft_ads.track_event",
            name="Track event",
            description="Compatibility alias for Microsoft Advertising conversion tracking.",
            input_schema={"type": "object", "additionalProperties": True},
            required_capabilities=("integrations.execute",),
        ),
        ConnectorActionDef(
            key="track_conversion",
            slug="microsoft_ads.track_conversion",
            name="Track conversion",
            description="Compatibility alias for Microsoft Advertising conversion upload.",
            input_schema={"type": "object", "additionalProperties": True},
            required_capabilities=("integrations.execute",),
        ),
    ),
    changelog=(
        ConnectorChangelogEntry(
            version="01.00.00",
            date="2026-04-26",
            notes="Initial release. Frontend UET tag injection + server-side offline conversion upload.",
        ),
        ConnectorChangelogEntry(
            version="01.01.00",
            date="2026-10-01",
            notes=(
                "Added required customer_id (manager account) field: the Offline Conversions "
                "API requires it as the CustomerId header, distinct from CustomerAccountId. "
                "Replaced the unmapped order_id input with the two Microsoft-supported "
                "external-attribution fields."
            ),
        ),
    ),
)
