import type { BookingDetail, BookingListRow } from "@/types/booking";

function labelFromRef(
  ref?: { name?: string; label?: string; code?: string; companyName?: string },
  fallback = "—",
): string {
  if (!ref) return fallback;
  return (
    ref.companyName ??
    ref.label ??
    ref.name ??
    ref.code ??
    fallback
  );
}

export function toBookingListRow(booking: BookingDetail): BookingListRow {
  return {
    id: booking.id,
    reference: booking.reference,
    approvalSteps: booking.approvalSteps,
    customerLabel: labelFromRef(booking.company, booking.companyId),
    // customerLabel: labelFromRef(booking.customer, booking.customerId),
    status: booking.status,
    scheduledDate: booking.scheduledDate,
    totalAmount: Number(booking.totalAmount),
    paymentCurrency: booking.paymentCurrency,
    paymentStatus: booking.paymentStatus,
    tradeDirection: booking.tradeDirection,
    freightType: booking.freightType,
    originLabel: labelFromRef(booking.originYard),
    destinationLabel: labelFromRef(booking.destinationYard),
    priorityScore: booking.priorityScore ?? 0,
    createdAt: booking.createdAt,
  };
}
