export const OVERVIEW_URGENT_PRIORITY_THRESHOLD = 70;

// ── Booking status groupings ─────────────────────────────────────────────────
// Derived from the current BookingStatus enum (@edr/types). "needs action" =
// anything sitting on a staff queue; "closed" = terminal states.

export const OVERVIEW_NEEDS_ACTION_STATUSES = [
  'SUBMITTED',
  'PENDING_APPROVAL',
  'APPROVED_PENDING_SIGNATURE',
  'AWAITING_DOCUMENTS',
  'DOCUMENTS_UNDER_REVIEW',
  'OPERATION_REQUEST_PENDING',
  'OPERATION_CHANGES_REQUESTED',
  'OPERATION_PRICE_PENDING_CONFIRM',
  'PENDING_CONSOLIDATION',
] as const;

export const OVERVIEW_IN_APPROVAL_STATUSES = [
  'PENDING_APPROVAL',
  'APPROVED_PENDING_SIGNATURE',
] as const;

export const OVERVIEW_CLOSED_STATUSES = [
  'REJECTED',
  'CANCELLED',
  'COMPLETED',
  'DELIVERED',
  'EXPIRED',
  'CONTRACT_CLOSED',
] as const;

// ── Contract status groupings ────────────────────────────────────────────────
// Derived from CONTRACT_STATUSES (@edr/types). Separate machine from bookings.

export const OVERVIEW_CONTRACT_NEEDS_ACTION_STATUSES = [
  'SUBMITTED',
  'PENDING_APPROVAL',
  'APPROVED_PENDING_SIGNATURE',
  'PRICE_CHANGED_PENDING_CONFIRM',
  'CHANGES_REQUESTED',
  'RENEWAL_SUBMITTED',
  'RENEWAL_PENDING_APPROVAL',
  'AMENDMENTS_PROPOSED',
] as const;

export const OVERVIEW_CONTRACT_IN_APPROVAL_STATUSES = [
  'PENDING_APPROVAL',
  'APPROVED_PENDING_SIGNATURE',
  'RENEWAL_PENDING_APPROVAL',
] as const;

export const OVERVIEW_CONTRACT_IN_CLEARANCE_STATUSES = [
  'AWAITING_CLEARANCE_DOCUMENTS',
  'CLEARANCE_UNDER_REVIEW',
  'CLEARANCE_READY_FOR_BOOKING',
] as const;

export const OVERVIEW_CONTRACT_CLOSED_STATUSES = [
  'CONTRACT_CLOSED',
  'EXPIRED',
  'REJECTED',
  'CANCELLED',
  'ARCHIVED',
] as const;

/** Contract pipeline stages for the status chart, in workflow order. */
export const OVERVIEW_CONTRACT_PIPELINE: ReadonlyArray<{
  stage: string;
  statuses: readonly string[];
}> = [
  { stage: 'draft', statuses: ['DRAFT', 'RENEWAL_DRAFT'] },
  {
    stage: 'intake',
    statuses: ['SUBMITTED', 'RENEWAL_SUBMITTED', 'PRICE_CHANGED_PENDING_CONFIRM', 'CHANGES_REQUESTED', 'AMENDMENTS_PROPOSED'],
  },
  {
    stage: 'in_approval',
    statuses: ['PENDING_APPROVAL', 'APPROVED', 'APPROVED_PENDING_SIGNATURE', 'RENEWAL_PENDING_APPROVAL'],
  },
  { stage: 'signing', statuses: ['CONTRACT_READY', 'SIGNED_CUSTOMER'] },
  {
    stage: 'clearance',
    statuses: ['AWAITING_CLEARANCE_DOCUMENTS', 'CLEARANCE_UNDER_REVIEW', 'CLEARANCE_READY_FOR_BOOKING'],
  },
  {
    stage: 'active',
    statuses: ['FULLY_EXECUTED', 'CONTRACT_ACTIVE', 'ACTIVE_SHIPMENT_IN_PROGRESS'],
  },
  { stage: 'closed', statuses: ['CONTRACT_CLOSED', 'EXPIRED', 'ARCHIVED'] },
  { stage: 'cancelled', statuses: ['REJECTED', 'CANCELLED'] },
];

/** Map a raw status→count record onto the contract pipeline stages. */
export function mapContractStatusCountsToPipeline(
  statusCounts: Record<string, number>,
): Array<{ stage: string; count: number }> {
  return OVERVIEW_CONTRACT_PIPELINE.map(({ stage, statuses }) => ({
    stage,
    count: statuses.reduce((sum, s) => sum + (statusCounts[s] ?? 0), 0),
  }));
}

export const OVERVIEW_RANGE_DAYS = {
  '7d': 7,
  '30d': 30,
  '90d': 90,
} as const;

export type OverviewRange = keyof typeof OVERVIEW_RANGE_DAYS;
