/**
 * Expense webhook emission — expenses-module.
 * Enqueues expense webhook events onto the webhook.deliver queue.
 */
import type { Env } from '@zync/types'

export type ExpenseWebhookEvent =
  | 'expense.uploaded'
  | 'expense.processed'
  | 'expense.failed'

interface WebhookEnvelope {
  type: 'webhook.deliver'
  tenantId: string
  event: string
  payload: Record<string, unknown>
  timestamp: string
}

export async function emitExpenseWebhook(
  env: Env,
  tenantId: string,
  event: ExpenseWebhookEvent,
  payload: Record<string, unknown>,
): Promise<void> {
  const envelope: WebhookEnvelope = {
    type: 'webhook.deliver',
    tenantId,
    event,
    payload,
    timestamp: new Date().toISOString(),
  }

  await env.QUEUE.send(envelope)
}
