// ─── Event Envelope Formatter ─────────────────────────────────────────────────
//
// Formats a platform event for injection into an agent's FIFO.
// The resulting string is used as the `text` argument to writeToAgentFifo().

export interface PlatformEventRef {
  id: string;
  topic: string;
  from: string;
  payload: unknown;
  ts: number;
}

/**
 * Format a platform event as a FIFO-ready message envelope.
 * Agents receive this as an incoming user message from the event bus.
 */
export function formatEventEnvelope(event: PlatformEventRef): string {
  const payloadStr =
    typeof event.payload === "string"
      ? event.payload
      : JSON.stringify(event.payload, null, 2);

  return [
    `<event type="${event.topic}" from="${event.from}" id="${event.id}" ts="${event.ts}">`,
    payloadStr,
    `</event>`,
    ``,
    `This is an autonomous platform event (topic: ${event.topic}, from: ${event.from}).`,
    `React according to your event subscription protocols. No human sent this — it was`,
    `emitted by the Teletraan event bus at ${new Date(event.ts).toISOString()}.`,
  ].join("\n");
}
