import { hmacSign } from './hmac';
import type { WebhookPayload } from './webhook-adapter';
import type { EmailInboundPayload } from './email-adapter';

/** Email-shaped WebhookPayload (legacy generic-webhook fixture). */
export const emailFixture: WebhookPayload = {
  eventId: 'email-1',
  from: 'someone@example.com',
  displayName: 'Someone',
  threadRef: 'mail-thread-1',
  subject: 'Question about my plot',
  text: 'Hello, I have a question.',
};

/** A first inbound email (starts a thread) for the EmailAdapter. */
export const emailInboundFixture: EmailInboundPayload = {
  messageId: '<m1@example.com>',
  from: 'buyer@example.com',
  fromName: 'Buyer',
  to: ['support@tower.example'],
  subject: 'Need help with my order',
  text: 'Hi, my order has not arrived yet.',
  occurredAt: '2026-07-03T10:00:00.000Z',
};

/** A reply to `emailInboundFixture` — must land on the SAME email thread. */
export const emailReplyFixture: EmailInboundPayload = {
  messageId: '<m2@example.com>',
  from: 'buyer@example.com',
  fromName: 'Buyer',
  to: ['support@tower.example'],
  subject: 'Re: Need help with my order',
  text: 'Any update on this?',
  inReplyTo: '<m1@example.com>',
  references: ['<m1@example.com>'],
  occurredAt: '2026-07-03T11:00:00.000Z',
};

/** WhatsApp-shaped provider payload. */
export const whatsappFixture: WebhookPayload = {
  eventId: 'wa-1',
  from: '+15551234567',
  displayName: 'WA User',
  threadRef: 'wa-group-1',
  text: 'hi from whatsapp',
};

/** Produce a signed request (body + headers) for tests/smoke. */
export function signedFixture(payload: object, secret: string): { body: string; headers: Record<string, string> } {
  const body = JSON.stringify(payload);
  return { body, headers: { 'x-iios-signature': hmacSign(body, secret), 'content-type': 'application/json' } };
}
