/**
 * Reusable webhook dispatch types.
 * Decoupled from any specific database — product backends provide their own persistence.
 */

export interface WebhookTarget {
  /** Unique ID for this webhook registration */
  id: string;
  /** URL to POST to */
  url: string;
  /** HMAC-SHA256 signing secret */
  secret: string;
  /** Event types this target subscribes to (empty = all) */
  events: string[];
  /** Whether this target is enabled */
  enabled: boolean;
}

export interface WebhookPayload {
  id: string;
  event: string;
  productId: string;
  timestamp: string;
  data: Record<string, unknown>;
}

export interface DeliveryAttempt {
  attemptNumber: number;
  responseCode?: number;
  durationMs: number;
  error?: string;
  attemptedAt: string;
}

export interface DeliveryResult {
  deliveryId: string;
  targetId: string;
  event: string;
  status: 'success' | 'failed';
  attempts: DeliveryAttempt[];
  completedAt: string;
}

export interface DispatchOptions {
  /** Max retries per target (default: 3) */
  maxRetries?: number;
  /** Timeout per attempt in ms (default: 5000) */
  timeoutMs?: number;
  /** Backoff intervals in ms (default: [10000, 60000, 300000]) */
  backoffIntervals?: number[];
  /** Called after each delivery completes (success or failure) */
  onDelivery?: (result: DeliveryResult) => void | Promise<void>;
}
