// Road Runner delivery statuses (plus our own pre-booking states) and how to present them.

export type DeliveryTone = 'neutral' | 'info' | 'progress' | 'success' | 'danger';

export const DELIVERY_STATUS: Record<string, { label: string; tone: DeliveryTone; hint: string }> = {
  not_booked: { label: 'Not booked', tone: 'neutral', hint: 'This order has not been sent to the courier yet.' },
  awaiting_booking: { label: 'Awaiting booking', tone: 'neutral', hint: 'Booked automatically with Road Runner once the order is paid.' },
  booking_failed: { label: 'Booking failed', tone: 'danger', hint: 'Road Runner rejected the booking. Fix the issue and book again.' },
  pending: { label: 'Booked', tone: 'info', hint: 'Road Runner accepted the delivery and is dispatching it.' },
  dispatched: { label: 'Dispatched', tone: 'info', hint: 'Sent to the courier network, waiting for a courier.' },
  dispatch_failed: { label: 'Dispatch failed', tone: 'danger', hint: 'The courier network could not take this delivery.' },
  assigned: { label: 'Courier assigned', tone: 'progress', hint: 'A courier is heading to the pickup point.' },
  picked_up: { label: 'Picked up', tone: 'progress', hint: 'The courier has collected the parcel.' },
  in_transit: { label: 'On the way', tone: 'progress', hint: 'The parcel is on its way to the customer.' },
  delivered: { label: 'Delivered', tone: 'success', hint: 'Delivered to the customer.' },
  failed: { label: 'Delivery failed', tone: 'danger', hint: 'The courier could not complete the delivery.' },
  cancelled: { label: 'Cancelled', tone: 'neutral', hint: 'This delivery was cancelled.' },
};

export const TONE_CLASSES: Record<DeliveryTone, string> = {
  neutral: 'bg-slate-500/10 text-slate-300 border-slate-500/20',
  info: 'bg-sky-500/10 text-sky-300 border-sky-500/20',
  progress: 'bg-amber-500/10 text-amber-300 border-amber-500/20',
  success: 'bg-emerald-500/10 text-emerald-300 border-emerald-500/20',
  danger: 'bg-rose-500/10 text-rose-300 border-rose-500/20',
};

export const TONE_DOT: Record<DeliveryTone, string> = {
  neutral: 'bg-slate-400',
  info: 'bg-sky-400',
  progress: 'bg-amber-400',
  success: 'bg-emerald-400',
  danger: 'bg-rose-400',
};

/** The happy path shown as a progress stepper. */
export const DELIVERY_STEPS = [
  { key: 'pending', label: 'Booked' },
  { key: 'dispatched', label: 'Dispatched' },
  { key: 'assigned', label: 'Courier assigned' },
  { key: 'picked_up', label: 'Picked up' },
  { key: 'in_transit', label: 'On the way' },
  { key: 'delivered', label: 'Delivered' },
];

export function deliveryStepIndex(status: string): number {
  return DELIVERY_STEPS.findIndex((s) => s.key === status);
}

export function statusMeta(status?: string | null) {
  return DELIVERY_STATUS[status || 'not_booked'] || { label: status || 'Unknown', tone: 'neutral' as DeliveryTone, hint: '' };
}
