/**
 * Inbound webhook idempotency — S7-i2-003.
 *
 * Short TTL KV dedup prevents duplicate queue enqueues when providers retry
 * deliveries (Meta, Slack, Resend).
 */
const INBOUND_DEDUP_TTL_SECONDS = 86400 // 24 hours

export async function claimInboundWebhookEvent(
  kv: KVNamespace,
  source: string,
  eventId: string,
): Promise<boolean> {
  if (!eventId) return true

  const key = `webhook:inbound:seen:${source}:${eventId}`
  const seen = await kv.get(key)
  if (seen) return false

  await kv.put(key, '1', { expirationTtl: INBOUND_DEDUP_TTL_SECONDS })
  return true
}
