export const BOOKING_LIST_TAB_KEYS = [
  'all',
  'intake',
  'in_approval',
  'approved_contract',
  'payment',
  'operations',
  'completed',
  'closed',
] as const;

export type BookingListTabKey = (typeof BOOKING_LIST_TAB_KEYS)[number];

export const BOOKING_LIST_TABS: ReadonlyArray<{
  key: BookingListTabKey;
  statuses: readonly string[] | null;
}> = [
  { key: 'all', statuses: null },
  { key: 'intake', statuses: ['SUBMITTED', 'PENDING_CONSOLIDATION'] },
  {
    key: 'in_approval',
    statuses: ['PENDING_APPROVAL', 'APPROVED_PENDING_SIGNATURE'],
  },
  {
    key: 'approved_contract',
    statuses: ['APPROVED', 'CONTRACT_READY', 'SIGNED_CUSTOMER', 'FULLY_EXECUTED'],
  },
  { key: 'payment', statuses: ['FULLY_EXECUTED'] },
  {
    key: 'operations',
    statuses: ['IN_TRANSIT', 'PAID'],
  },
  { key: 'completed', statuses: ['COMPLETED'] },
  { key: 'closed', statuses: ['REJECTED', 'CANCELLED'] },
];

export function mapStatusCountsToTabs(
  statusCounts: Record<string, number>,
): Record<BookingListTabKey, number> {
  const result = {} as Record<BookingListTabKey, number>;

  for (const tab of BOOKING_LIST_TABS) {
    if (!tab.statuses?.length) {
      result[tab.key] = Object.values(statusCounts).reduce((sum, n) => sum + n, 0);
      continue;
    }
    result[tab.key] = tab.statuses.reduce(
      (sum, status) => sum + (statusCounts[status] ?? 0),
      0,
    );
  }

  return result;
}
