/**
 * CloudEvents envelope + the IIOS event catalog (Data Model §05, Ref Arch §09).
 *
 * Every durable state change writes the business row AND an outbox event in the
 * same transaction. The `insignia` block carries correlation/causation/scope and
 * the idempotency key; payloads reference content rather than embedding raw PII.
 */

export type DataClass = 'internal' | 'restricted' | 'confidential' | 'regulated';

export interface CloudEvent<T = unknown> {
  specversion: '1.0';
  id: string;
  type: string;
  source: string;
  subject?: string;
  time: string;
  datacontenttype?: 'application/json';
  traceparent?: string;
  insignia: {
    correlationId?: string;
    causationId?: string;
    scopeSnapshotId?: string;
    policyDecisionId?: string;
    consentReceiptRef?: string;
    contextBundleId?: string;
    idempotencyKey: string;
    dataClass?: DataClass;
  };
  data: T;
}

/** Canonical event names (the subset relevant up to P4; others added per phase). */
export const IIOS_EVENTS = {
  rawReceived: 'com.insignia.iios.raw.received.v1',
  interactionNormalized: 'com.insignia.iios.interaction.normalized.v1',
  messageSent: 'com.insignia.iios.message.sent.v1',
  messageRead: 'com.insignia.iios.message.read.v1',
  messagePersisted: 'com.insignia.iios.message.persisted.v1',
  inboxItemCreated: 'com.insignia.iios.inbox.item.created.v1',
  ticketCreated: 'com.insignia.iios.support.ticket.created.v1',
  ticketStateChanged: 'com.insignia.iios.support.ticket.state.changed.v1',
  callbackRequested: 'com.insignia.iios.support.callback.requested.v1',
  routeDecisionPreviewed: 'com.insignia.iios.route.decision.previewed.v1',
  routeApproved: 'com.insignia.iios.route.approved.v1',
  aiJobCreated: 'com.insignia.iios.ai.job.created.v1',
  aiArtifactProposed: 'com.insignia.iios.ai.artifact.proposed.v1',
  aiArtifactAccepted: 'com.insignia.iios.ai.artifact.accepted.v1',
  meetingRequested: 'com.insignia.iios.calendar.meeting.requested.v1',
  meetingScheduled: 'com.insignia.iios.calendar.meeting.scheduled.v1',
  meetingReminderQueued: 'com.insignia.iios.calendar.reminder.queued.v1',
  meetingSummarized: 'com.insignia.iios.calendar.meeting.summarized.v1',
  notificationQueued: 'com.insignia.iios.notification.queued.v1',
  deliveryAttempted: 'com.insignia.iios.delivery.attempted.v1',
  devChaos: 'com.insignia.iios.dev.chaos.v1',
} as const;

export type IiosEventType = (typeof IIOS_EVENTS)[keyof typeof IIOS_EVENTS];
