asyncapi: '2.6.0'
info:
  title: leadgen-svc Async API
  version: '2.1.0'
  description: |
    Kafka event contracts for leadgen-svc.
    Topic pattern: eventzr.leadgen.<entity>.<action>.v1
    Format: CloudEvents 1.0
    Partition key: tenant_id
  contact:
    name: EventZR Backend Team
  license:
    name: Proprietary

servers:
  staging:
    url: boot-gpm4u49z.c2.kafka-serverless.us-east-1.amazonaws.com:9098
    protocol: kafka
    description: AWS MSK Serverless (staging)

defaultContentType: application/cloudevents+json

channels:
  # ─── Produced Events (19) ───────────────────────────────────
  eventzr.leadgen.lead.created.v1:
    publish:
      summary: Lead created via authenticated endpoint
      operationId: publishLeadCreated
      message:
        $ref: '#/components/messages/LeadCreated'

  eventzr.leadgen.lead.updated.v1:
    publish:
      summary: Lead fields updated
      operationId: publishLeadUpdated
      message:
        $ref: '#/components/messages/LeadUpdated'

  eventzr.leadgen.lead.deleted.v1:
    publish:
      summary: Lead archived/deleted
      operationId: publishLeadDeleted
      message:
        $ref: '#/components/messages/LeadDeleted'

  eventzr.leadgen.lead.captured.v1:
    publish:
      summary: Lead captured via public capture endpoint
      operationId: publishLeadCaptured
      message:
        $ref: '#/components/messages/LeadCaptured'

  eventzr.leadgen.lead.stage-changed.v1:
    publish:
      summary: Lead moved to new pipeline stage
      operationId: publishLeadStageChanged
      message:
        $ref: '#/components/messages/LeadStageChanged'

  eventzr.leadgen.deal.created.v1:
    publish:
      summary: New deal created
      operationId: publishDealCreated
      message:
        $ref: '#/components/messages/DealCreated'

  eventzr.leadgen.deal.stage-changed.v1:
    publish:
      summary: Deal moved to new stage
      operationId: publishDealStageChanged
      message:
        $ref: '#/components/messages/DealStageChanged'

  eventzr.leadgen.deal.won.v1:
    publish:
      summary: Deal marked as won
      operationId: publishDealWon
      message:
        $ref: '#/components/messages/DealWon'

  eventzr.leadgen.deal.lost.v1:
    publish:
      summary: Deal marked as lost
      operationId: publishDealLost
      message:
        $ref: '#/components/messages/DealLost'

  eventzr.leadgen.nurture.enrolled.v1:
    publish:
      summary: Lead enrolled in nurture sequence
      operationId: publishNurtureEnrolled
      message:
        $ref: '#/components/messages/NurtureEnrolled'

  eventzr.leadgen.nurture.step-executed.v1:
    publish:
      summary: Nurture sequence step executed
      operationId: publishNurtureStepExecuted
      message:
        $ref: '#/components/messages/NurtureStepExecuted'

  eventzr.leadgen.nurture.completed.v1:
    publish:
      summary: Lead completed nurture sequence
      operationId: publishNurtureCompleted
      message:
        $ref: '#/components/messages/NurtureCompleted'

  eventzr.leadgen.crm.synced.v1:
    publish:
      summary: CRM synchronization completed
      operationId: publishCrmSynced
      message:
        $ref: '#/components/messages/CrmSynced'

  eventzr.leadgen.crm.conflict.v1:
    publish:
      summary: CRM sync conflict detected
      operationId: publishCrmConflict
      message:
        $ref: '#/components/messages/CrmConflict'

  eventzr.leadgen.payment.completed.v1:
    publish:
      summary: Form payment completed
      operationId: publishPaymentCompleted
      message:
        $ref: '#/components/messages/PaymentCompleted'

  eventzr.leadgen.segment.computed.v1:
    publish:
      summary: Segment membership re-computed
      operationId: publishSegmentComputed
      message:
        $ref: '#/components/messages/SegmentComputed'

  eventzr.leadgen.form.published.v1:
    publish:
      summary: Form published
      operationId: publishFormPublished
      message:
        $ref: '#/components/messages/FormPublished'

  eventzr.leadgen.form.unpublished.v1:
    publish:
      summary: Form unpublished
      operationId: publishFormUnpublished
      message:
        $ref: '#/components/messages/FormUnpublished'

  eventzr.leadgen.tool.registered.v1:
    publish:
      summary: MCP tool registered
      operationId: publishToolRegistered
      message:
        $ref: '#/components/messages/ToolRegistered'

  # ─── Consumed Events (12) ──────────────────────────────────
  eventzr.event.created.v1:
    subscribe:
      summary: Event created - auto-create pipeline if applicable
      operationId: consumeEventCreated
      message:
        $ref: '#/components/messages/EventCreated'

  eventzr.venue.created.v1:
    subscribe:
      summary: Venue created - update enrichment data
      operationId: consumeVenueCreated
      message:
        $ref: '#/components/messages/VenueCreated'

  eventzr.sphere.post.engagement-updated.v1:
    subscribe:
      summary: Social engagement updated - update lead scoring signals
      operationId: consumeSphereEngagement
      message:
        $ref: '#/components/messages/SphereEngagement'

  eventzr.notify.delivery-confirmed.v1:
    subscribe:
      summary: Notification delivery confirmed - update nurture step status
      operationId: consumeNotifyDelivery
      message:
        $ref: '#/components/messages/NotifyDelivery'

  eventzr.analytics.content-performance.v1:
    subscribe:
      summary: Content performance data - update conversion predictions
      operationId: consumeContentPerformance
      message:
        $ref: '#/components/messages/ContentPerformance'

  eventzr.crm.contact.updated.v1:
    subscribe:
      summary: CRM contact updated - sync back to lead
      operationId: consumeCrmContactUpdated
      message:
        $ref: '#/components/messages/CrmContactUpdated'

  eventzr.crm.deal.updated.v1:
    subscribe:
      summary: CRM deal updated - sync back to local deal
      operationId: consumeCrmDealUpdated
      message:
        $ref: '#/components/messages/CrmDealUpdated'

  eventzr.support.ticket.created.v1:
    subscribe:
      summary: Support ticket created - add to lead activity
      operationId: consumeSupportTicketCreated
      message:
        $ref: '#/components/messages/SupportTicketCreated'

  eventzr.crm.contact.created.v1:
    subscribe:
      summary: CRM contact created from lead - backfill crmContactRef
      operationId: consumeCrmContactCreated
      message:
        $ref: '#/components/messages/CrmContactCreated'

  eventzr.webify.form.updated.v1:
    subscribe:
      summary: Form schema updated - invalidate cache
      operationId: consumeWebifyFormUpdated
      message:
        $ref: '#/components/messages/WebifyFormUpdated'

  eventzr.user.deleted.v1:
    subscribe:
      summary: User deleted (GDPR) - anonymize leads
      operationId: consumeUserDeleted
      message:
        $ref: '#/components/messages/UserDeleted'

  eventzr.wallet.credits.depleted.v1:
    subscribe:
      summary: Credits depleted - disable auto-enrichment
      operationId: consumeCreditsDepleted
      message:
        $ref: '#/components/messages/CreditsDepleted'

components:
  messages:
    LeadCreated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          leadId: { type: string }
          email: { type: string }
          source: { type: string }
          createdAt: { type: string, format: date-time }

    LeadUpdated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          leadId: { type: string }
          updatedFields: { type: array, items: { type: string } }
          updatedAt: { type: string, format: date-time }

    LeadDeleted:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          leadId: { type: string }
          deletedAt: { type: string, format: date-time }

    LeadCaptured:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          leadId: { type: string }
          formId: { type: string }
          source: { type: string }
          email: { type: string }
          phoneE164: { type: string }
          walletEip155: { type: string }
          digest: { type: string }
          capturedAt: { type: string, format: date-time }

    LeadStageChanged:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          leadId: { type: string }
          previousStageId: { type: string }
          newStageId: { type: string }
          changedBy: { type: string }
          changedAt: { type: string, format: date-time }

    DealCreated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          dealId: { type: string }
          pipelineId: { type: string }
          stageId: { type: string }
          value: { type: number }
          leadId: { type: string }
          createdAt: { type: string, format: date-time }

    DealStageChanged:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          dealId: { type: string }
          previousStageId: { type: string }
          newStageId: { type: string }
          changedBy: { type: string }

    DealWon:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          dealId: { type: string }
          value: { type: number }
          wonBy: { type: string }
          wonAt: { type: string, format: date-time }

    DealLost:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          dealId: { type: string }
          lostReason: { type: string }
          competitorName: { type: string }
          lostAt: { type: string, format: date-time }

    NurtureEnrolled:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          sequenceId: { type: string }
          leadId: { type: string }
          enrolledBy: { type: string }
          enrolledAt: { type: string, format: date-time }

    NurtureStepExecuted:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          sequenceId: { type: string }
          leadId: { type: string }
          stepIndex: { type: integer }
          stepType: { type: string }
          executedAt: { type: string, format: date-time }

    NurtureCompleted:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          sequenceId: { type: string }
          leadId: { type: string }
          completedAt: { type: string, format: date-time }

    CrmSynced:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          connectionId: { type: string }
          direction: { type: string }
          recordsSynced: { type: integer }
          syncedAt: { type: string, format: date-time }

    CrmConflict:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          connectionId: { type: string }
          entityType: { type: string }
          entityId: { type: string }
          conflictType: { type: string }

    PaymentCompleted:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          paymentId: { type: string }
          formId: { type: string }
          amount: { type: number }
          currency: { type: string }

    SegmentComputed:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          segmentId: { type: string }
          memberCount: { type: integer }
          computedAt: { type: string, format: date-time }

    FormPublished:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          formId: { type: string }
          publishedAt: { type: string, format: date-time }

    FormUnpublished:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          formId: { type: string }
          unpublishedAt: { type: string, format: date-time }

    ToolRegistered:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          toolName: { type: string }
          toolId: { type: string }

    EventCreated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          eventId: { type: string }
          title: { type: string }

    VenueCreated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          venueId: { type: string }

    SphereEngagement:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          postId: { type: string }
          engagementType: { type: string }

    NotifyDelivery:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          notificationId: { type: string }
          status: { type: string }

    ContentPerformance:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          contentId: { type: string }
          metrics: { type: object }

    CrmContactUpdated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          contactId: { type: string }
          updatedFields: { type: object }

    CrmDealUpdated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          dealId: { type: string }
          updatedFields: { type: object }

    SupportTicketCreated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          ticketId: { type: string }
          leadId: { type: string }

    CrmContactCreated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          contactId: { type: string }
          leadId: { type: string }

    WebifyFormUpdated:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          formId: { type: string }

    UserDeleted:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          userId: { type: string }
          deletedAt: { type: string, format: date-time }

    CreditsDepleted:
      payload:
        type: object
        properties:
          tenantId: { type: string }
          walletId: { type: string }
          remainingCredits: { type: number }
