import {
  WAREHOUSE_TYPES,
  WAREHOUSE_YARD_TYPES,
  WAREHOUSE_ZONE_TYPES,
  WAREHOUSE_STATUSES,
  INVENTORY_STATUSES,
} from '@/types/warehouse';

export const humanizeEnum = (value: string) =>
  value
    .toLowerCase()
    .split('_')
    .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
    .join(' ');

const toOptions = (values: readonly string[]) =>
  values.map((value) => ({ value, label: humanizeEnum(value) }));

export const warehouseTypeOptions = toOptions(WAREHOUSE_TYPES);
export const yardTypeOptions = toOptions(WAREHOUSE_YARD_TYPES);
export const zoneTypeOptions = toOptions(WAREHOUSE_ZONE_TYPES);
export const statusOptions = toOptions(WAREHOUSE_STATUSES);
export const inventoryStatusOptions = toOptions(INVENTORY_STATUSES);

export const formatNumber = (value: number | null | undefined) => {
  if (value === null || value === undefined) return '—';
  const num = Number(value);
  if (Number.isNaN(num)) return '—';
  return num.toLocaleString(undefined, { maximumFractionDigits: 3 });
};

export const formatCapacity = (current: number, capacity: number | null | undefined) => {
  const cur = formatNumber(current);
  if (capacity === null || capacity === undefined) return cur;
  return `${cur} / ${formatNumber(capacity)}`;
};

export const formatDate = (value: string | null | undefined) => {
  if (!value) return '—';
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return '—';
  return date.toLocaleString(undefined, {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  });
};

export const extractErrorMessage = (error: unknown, fallback = 'Something went wrong') => {
  const responseData = (error as { response?: { data?: unknown } })?.response?.data;
  const data = responseData && typeof responseData === 'object' ? (responseData as Record<string, unknown>) : undefined;
  const rawMessage = data?.message ?? data?.error ?? (error as Error)?.message;
  return Array.isArray(rawMessage) ? rawMessage.join(', ') : rawMessage ? String(rawMessage) : fallback;
};
