/**
 * In-product notification types.
 *
 * Notifications come from two sources:
 *
 *   - **Per-user notifications** (the original kind) — durable rows in
 *     the `notifications` collection, generated by domain events (audit
 *     completed, quota granted, etc.) and addressed to one specific
 *     recipient. v1 is in-product only — email / push live outside
 *     this module.
 *
 *   - **Broadcasts** (#500) — admin-authored bilingual markdown
 *     messages that land in *every* user's inbox. Stored in the
 *     `broadcasts` collection; per-user read state lives in
 *     `broadcast_read_receipts`. The notifications service merges
 *     them into the same feed the bell already renders, distinguished
 *     by a `source: "broadcast"` discriminator and the bilingual
 *     `titleI18n` / `bodyMarkdownI18n` payload.
 *
 * @module domains/notifications/types
 */

export type NotificationCategory =
  | "audit.completed"
  | "audit.risky_for_consumer"
  | "quota.credits_granted";

export interface NotificationDocument {
  readonly _id: string;
  /** Recipient user id (NyxID user_id). */
  readonly userId: string;
  readonly category: NotificationCategory;
  /** One-line summary for list view. */
  readonly title: string;
  /** Optional longer body rendered on the detail view. Plain text. */
  readonly body?: string;
  /** Deep-link path in the web UI, e.g. `/skills/abc/audits?version=1.0.0`. */
  readonly link?: string;
  /** Arbitrary structured payload the UI can use (e.g. `{ skillGuid, version, verdict }`). */
  readonly data: Record<string, unknown>;
  readonly readAt?: Date | null;
  readonly createdAt: Date;
}

/**
 * Discriminated feed item returned by `/api/v1/notifications`. The
 * `source` field tells the UI how to render: `"user"` rows carry the
 * existing single-string `title` / `body` shape; `"broadcast"` rows
 * carry bilingual markdown that the frontend resolves at render time.
 *
 * The two shapes share `_id`, `createdAt`, and `readAt` so the bell's
 * existing sort + read-state machinery works uniformly across both.
 */
export type FeedItem =
  | (NotificationDocument & { readonly source: "user" })
  | {
      readonly _id: string;
      readonly source: "broadcast";
      readonly titleI18n: { en: string; zh: string };
      readonly bodyMarkdownI18n: { en: string; zh: string };
      readonly createdAt: Date;
      readonly readAt: Date | null;
    };
