/**
 * Canonical webhook event catalog — webhook-endpoint-detail (wave-11 leaf-D).
 *
 * WEBHOOK_EVENT_CATALOG: every event Zync can dispatch.
 * WEBHOOK_EVENT_TYPES: flat array of type strings for validation.
 *
 * payloadShape is a representative sample payload (used for test dispatch).
 */

export interface WebhookEventDef {
  type: string
  description: string
  payloadShape: Record<string, unknown>
}

export const WEBHOOK_EVENT_CATALOG: WebhookEventDef[] = [
  // ── Invoice events ───────────────────────────────────────────────────────────
  {
    type: 'invoice.created',
    description: 'A new invoice was created',
    payloadShape: {
      id: 'inv_00000000-0000-0000-0000-000000000001',
      invoice_number: 'INV-2026-001',
      status: 'draft',
      total_amount: '1000.0000',
      customer_id: '00000000-0000-0000-0000-000000000001',
      created_at: '2026-06-01T12:00:00Z',
    },
  },
  {
    type: 'invoice.sent',
    description: 'An invoice was sent to the customer',
    payloadShape: {
      id: 'inv_00000000-0000-0000-0000-000000000001',
      invoice_number: 'INV-2026-001',
      status: 'sent',
      sent_at: '2026-06-01T12:00:00Z',
    },
  },
  {
    type: 'invoice.paid',
    description: 'An invoice was marked as paid',
    payloadShape: {
      id: 'inv_00000000-0000-0000-0000-000000000001',
      invoice_number: 'INV-2026-001',
      status: 'paid',
      paid_at: '2026-06-01T12:00:00Z',
      amount_paid: '1000.0000',
    },
  },
  {
    type: 'invoice.overdue',
    description: 'An invoice became overdue',
    payloadShape: {
      id: 'inv_00000000-0000-0000-0000-000000000001',
      invoice_number: 'INV-2026-001',
      status: 'overdue',
      due_date: '2026-05-01',
    },
  },
  {
    type: 'invoice.cancelled',
    description: 'An invoice was cancelled',
    payloadShape: {
      id: 'inv_00000000-0000-0000-0000-000000000001',
      invoice_number: 'INV-2026-001',
      status: 'cancelled',
    },
  },

  // ── Customer events ──────────────────────────────────────────────────────────
  {
    type: 'customer.created',
    description: 'A new customer was created',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      name: 'Acme Corp',
      email: 'contact@acme.com',
      created_at: '2026-06-01T12:00:00Z',
    },
  },
  {
    type: 'customer.updated',
    description: 'A customer record was updated',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      name: 'Acme Corp',
      updated_at: '2026-06-01T12:00:00Z',
    },
  },

  // ── Task events ──────────────────────────────────────────────────────────────
  {
    type: 'task.created',
    description: 'A new task was created',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      title: 'Example task',
      priority: 'medium',
      source: 'manual',
      created_at: '2026-06-01T12:00:00Z',
    },
  },
  {
    type: 'task.updated',
    description: 'A task was updated',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      title: 'Example task',
      status_id: '00000000-0000-0000-0000-000000000002',
      updated_at: '2026-06-01T12:00:00Z',
    },
  },
  {
    type: 'task.completed',
    description: 'A task was moved to a terminal status',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      title: 'Example task',
      completed_at: '2026-06-01T12:00:00Z',
    },
  },

  // ── Payment events ───────────────────────────────────────────────────────────
  {
    type: 'payment.received',
    description: 'A payment was received',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      amount: '1000.0000',
      currency: 'ILS',
      invoice_id: '00000000-0000-0000-0000-000000000001',
      received_at: '2026-06-01T12:00:00Z',
    },
  },

  // ── Proposal events ──────────────────────────────────────────────────────────
  {
    type: 'proposal.accepted',
    description: 'A proposal was accepted by the customer',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      title: 'Example Proposal',
      accepted_at: '2026-06-01T12:00:00Z',
    },
  },
  {
    type: 'proposal.declined',
    description: 'A proposal was declined by the customer',
    payloadShape: {
      id: '00000000-0000-0000-0000-000000000001',
      title: 'Example Proposal',
      declined_at: '2026-06-01T12:00:00Z',
    },
  },

  // ── Auto-pause events ────────────────────────────────────────────────────────
  {
    type: 'endpoint.auto_paused',
    description: 'An endpoint was automatically paused after repeated delivery failures',
    payloadShape: {
      endpoint_id: '00000000-0000-0000-0000-000000000001',
      url: 'https://hooks.example.com/...',
      paused_at: '2026-06-01T12:00:00Z',
    },
  },
]

export const WEBHOOK_EVENT_TYPES: string[] = WEBHOOK_EVENT_CATALOG.map((e) => e.type)

/**
 * Returns true if the endpoint's events list covers the given event type.
 * An empty events array means "all events" — subscribed to everything.
 */
export function isEndpointSubscribed(
  endpointEvents: unknown,
  eventType: string,
): boolean {
  if (!Array.isArray(endpointEvents) || endpointEvents.length === 0) return true
  return (endpointEvents as string[]).includes(eventType)
}
