/**
 * Canonical labels + StatusPill tones for the signing-document lifecycle.
 *
 * Six surfaces previously carried divergent label sets (interest-eoi-tab,
 * interest-contract-tab, interest-reservation-tab, documents-hub,
 * signing-progress, notification-digest, realtime-toast). A signer would
 * see "Partially signed", "partially_signed", and "EOI fully signed" for
 * the same enum state across one session. This module is the single
 * source of truth - import from here, do not redefine inline.
 *
 * If a new lifecycle state arrives in the schema, add it here once.
 */

import type { StatusPillStatus } from '@/components/ui/status-pill';

export type DocumentStatus =
  | 'draft'
  | 'sent'
  | 'partially_signed'
  | 'completed'
  | 'expired'
  | 'cancelled'
  // §4.13: Documenso writes 'rejected' (DOCUMENT_REJECTED webhook) and
  // some legacy paths use 'declined' for the same outcome. Surface both
  // so the rejection-banner check in interest-eoi-tab type-checks.
  | 'rejected'
  | 'declined';

/**
 * Human label rendered in CRM UI (staff-facing). Use the portal-specific
 * mapping in `documentStatusLabelForPortal` when rendering to clients -
 * "Awaiting signatures" reads fine on the inside; clients want
 * "Awaiting your signature".
 */
export const DOCUMENT_STATUS_LABELS: Record<DocumentStatus, string> = {
  draft: 'Draft',
  sent: 'Awaiting signatures',
  partially_signed: 'Partially signed',
  completed: 'Signed',
  expired: 'Expired',
  cancelled: 'Cancelled',
  rejected: 'Declined',
  declined: 'Declined',
};

/**
 * Client-portal labels. The portal previously rendered
 * `eoiStatus.replace(/_/g, ' ')` so a client saw "EOI: partially signed".
 * Map to action-oriented copy that the client can act on.
 */
export const DOCUMENT_STATUS_LABELS_PORTAL: Record<DocumentStatus, string> = {
  draft: 'Pending',
  sent: 'Awaiting your signature',
  partially_signed: 'Signed (other parties remaining)',
  completed: 'Signed',
  expired: 'Expired',
  cancelled: 'Cancelled',
  rejected: 'Declined',
  declined: 'Declined',
};

/**
 * StatusPill variant per state. Pairs with `<StatusPill status={...}>`
 * via the shared primitive so the colour palette stays consistent with
 * non-document status pills (berth/user/etc).
 */
export const DOCUMENT_STATUS_PILL: Record<DocumentStatus, StatusPillStatus> = {
  draft: 'draft',
  sent: 'sent',
  partially_signed: 'partial',
  completed: 'completed',
  expired: 'expired',
  cancelled: 'cancelled',
  rejected: 'rejected',
  declined: 'declined',
};

/**
 * The "in-flight" set - useful for hero treatment, banners, "we're
 * waiting on action" UI. completed/expired/cancelled are terminal.
 */
export const DOCUMENT_STATUS_ACTIVE: ReadonlySet<DocumentStatus> = new Set<DocumentStatus>([
  'draft',
  'sent',
  'partially_signed',
]);

export function documentStatusLabel(status: string): string {
  return DOCUMENT_STATUS_LABELS[status as DocumentStatus] ?? status;
}

export function documentStatusLabelForPortal(status: string): string {
  return DOCUMENT_STATUS_LABELS_PORTAL[status as DocumentStatus] ?? status;
}

export function documentStatusPill(status: string): StatusPillStatus {
  return DOCUMENT_STATUS_PILL[status as DocumentStatus] ?? 'pending';
}
