// Shared formatting helpers for the ordering management console. Kept tiny and
// dependency-free so each split route page (orders, products, pricing, …) can
// import them instead of re-declaring the same money/label functions.

const AUD = new Intl.NumberFormat('en-AU', { style: 'currency', currency: 'AUD' });

/** Format a value as AUD, or an em dash when null/undefined. */
export function money(value: number | null | undefined): string {
  if (value == null) return '—';
  return AUD.format(value);
}

/** Turn a snake_case status/category key into Title Case for display. */
export function label(value: string): string {
  return value.replace(/_/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
}

/** Order workflow statuses, in the order they typically progress. */
export const ORDER_STATUSES = [
  'submitted',
  'under_review',
  'confirmed',
  'sent_to_xero',
  'in_production',
  'ready_for_pickup',
  'dispatched',
  'completed',
  'cancelled'
] as const;

/**
 * Map an order / customer status to a pill tone class so states are scannable
 * at a glance: warn = needs attention, info = in flight, pos = done/active,
 * danger = cancelled. Unknown values fall back to the neutral base pill.
 */
const STATUS_TONE: Record<string, string> = {
  submitted: 'warn',
  under_review: 'warn',
  confirmed: 'pos',
  completed: 'pos',
  active: 'pos',
  cancelled: 'danger',
  disabled: 'danger',
  suspended: 'danger',
  sent_to_xero: 'info',
  in_production: 'info',
  ready_for_pickup: 'info',
  dispatched: 'info'
};

export function statusTone(value: string): string {
  return STATUS_TONE[value] ?? '';
}

/** Catalogue product categories. */
export const PRODUCT_CATEGORIES = [
  'grains',
  'premixed',
  'bags',
  'bulk_loads',
  'custom_blends',
  'services'
] as const;
