export type NotificationType = 'assignment' | 'mention' | 'risk_alert' | 'overdue_item';

export interface NotificationRecord {
  id: string;
  recipientId: string;
  actorId?: string | null;
  type: NotificationType;
  title: string;
  body?: string;
  resourceType: string;
  resourceId: string;
  resourceVersionId?: string | null;
  productId?: string | null;
  projectId?: string | null;
  versionId?: string | null;
  metadata?: Record<string, unknown>;
  readAt?: string | null;
  createdAt: string;
}

export function countUnreadNotifications(items: Pick<NotificationRecord, 'readAt'>[]): number {
  return items.filter((item) => !item.readAt).length;
}

export function sortNotificationsNewestFirst<T extends Pick<NotificationRecord, 'createdAt'>>(items: T[]): T[] {
  return [...items].sort((a, b) => dateValue(b.createdAt) - dateValue(a.createdAt));
}

function dateValue(value: string): number {
  const time = new Date(value).getTime();
  return Number.isFinite(time) ? time : 0;
}
