import { CommunicationTypeEnum } from 'src/common/enum/communication-type.enum';
import { SessionCommunicationPurposeEnum } from 'src/common/enum/session-communication-purpose.enum';
import { SessionCommunicationStatusEnum } from 'src/common/enum/session-communication-status.enum';
import { SessionOccurrence } from './session-communication.constants';

/**
 * A single grouped count row derived from the shared hdb_communication_track table.
 * `occurrence` distinguishes first- vs final-session variants (null for Value Card).
 */
export interface SessionCommunicationSummaryRow {
  registrationId: number;
  purpose: SessionCommunicationPurposeEnum;
  occurrence: SessionOccurrence | null;
  channel: CommunicationTypeEnum;
  count: number;
  lastSentAt: Date | string;
}

/**
 * Latest bulk-communication outcome for one (session, purpose), projected from the newest
 * hdb_session_communication_status row. Owned here (this domain writes those rows); consumed by
 * the online-session GET responses and the general-attendees analytics response. null at the call
 * site means the send has never run.
 */
export interface SessionCommunicationStatusSummary {
  status: SessionCommunicationStatusEnum;
  requested: number;
  emailSent: number;
  whatsappSent: number;
  skipped: number;
  lastTriggeredAt: Date;
}

/**
 * Reason a recipient/channel was skipped during a send.
 */
export interface SkippedRecipient {
  registrationId: number;
  channel: CommunicationTypeEnum;
  reason: string;
}

/**
 * Aggregate result of a bulk/single send.
 */
export interface SendResult {
  requested: number;
  enqueued: { email: number; whatsapp: number };
  skipped: SkippedRecipient[];
  /**
   * True when the send was ACCEPTED and is being processed off-request — the recipients were
   * resolved and counted into `requested`, but nothing has been dispatched yet, so `enqueued` is
   * all zeros and `skipped` is empty. Absent on sends that completed within the request.
   *
   * Set by the bulk endpoint, whose per-recipient merge resolution is too slow to hold an HTTP
   * request open on a large audience (it was timing out as a 502). The real outcome lands in
   * hdb_session_communication_status — the same row the online-session GET responses surface as
   * `communications`.
   */
  accepted?: boolean;
}
