openapi: 3.1.0
info:
  title: CRM Event Webhook
  version: "1.0.0"
  description: >
    Forward lead lifecycle events from the CRM. Send raw fields; hashing,
    lead matching and dispatch to the analytics provider happen server-side.
servers:
  - url: https://mc3-meta-leadform-sync.xvak3803014.workers.dev
security:
  - bearerAuth: []
paths:
  /webhooks/crm-event:
    post:
      summary: Report a lead event
      operationId: reportLeadEvent
      description: Call once per status change. Idempotent per (lead, event).
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Event"
            examples:
              qualified:
                value:
                  event: qualified
                  email: lead@example.com
                  phone: "48555123456"
                  name: Jan Kowalski
                  lead_id: "62895772-4a1d-473a-a0c4-10e88c4987b6"
                  source: facebook
              purchase:
                value:
                  event: purchase
                  email: lead@example.com
                  value: 9900
                  source: facebook
              google:
                value:
                  event: qualified
                  email: lead@example.com
                  source: google
      responses:
        "200":
          description: Accepted, or intentionally skipped.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/EventResult"
        "400":
          description: Invalid JSON or unknown event.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "403":
          description: Missing or invalid bearer token.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        "422":
          description: Neither email nor phone provided.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
  schemas:
    Event:
      type: object
      required: [event]
      additionalProperties: true
      properties:
        event:
          type: string
          enum: [contacted, qualified, scheduled, purchase]
        email:
          type: string
          description: Raw email. Required if phone absent.
        phone:
          type: string
          description: Raw phone, any format.
        name:
          type: string
        lead_id:
          type: string
          description: Your record id.
        source:
          type: string
          description: Lead source. "google" is stored, not dispatched.
        value:
          type: number
          description: Purchase amount (PLN). Defaults to 14575.
        occurred_at:
          type: string
          format: date-time
    EventResult:
      type: object
      properties:
        ok: { type: boolean }
        event: { type: string }
        metaEvent: { type: string }
        meta_event_id: { type: string }
        route: { type: string }
        enriched: { type: boolean }
        status: { type: integer }
        skipped: { type: string }
    Error:
      type: object
      properties:
        ok: { type: boolean }
        error: { type: string }
