import { Badge, Group, Text } from "@mantine/core";
import type { Freight } from "@edr/types";

/**
 * Shared presentation helpers for booking-like rows (one-time bookings AND
 * general contracts). Kept in one place so the bookings list, contracts list,
 * and detail page render type/freight/mode/payment consistently.
 */

type BookingLike = Freight.IBooking & {
  bookingType?: string;
  freightType?: string;
  tradeDirection?: string;
  paymentStatus?: string;
};

/** One-Time vs General Contract. */
export function BookingTypeBadge({ booking }: { booking: BookingLike }) {
  const isContract = booking.bookingType === "GENERAL_CONTRACT";
  return (
    <Badge
      variant="light"
      radius="sm"
      color={isContract ? "violet" : "gray"}
      styles={{ root: { textTransform: "none", fontWeight: 600, letterSpacing: 0 } }}
    >
      {isContract ? "General Contract" : "One-Time"}
    </Badge>
  );
}

/** Containerised vs Bulk, plus the trade direction (Import/Export/Domestic). */
export function CargoModeCell({ booking }: { booking: BookingLike }) {
  const freight =
    booking.freightType === "BULK" ? "Bulk" : "Container";
  const dir = booking.tradeDirection
    ? booking.tradeDirection.charAt(0) + booking.tradeDirection.slice(1).toLowerCase()
    : null;
  return (
    <Group gap={6} wrap="nowrap">
      <Badge
        variant="light"
        radius="sm"
        color={booking.freightType === "BULK" ? "orange" : "teal"}
        styles={{ root: { textTransform: "none", fontWeight: 600, letterSpacing: 0 } }}
      >
        {freight}
      </Badge>
      {dir && (
        <Text fz={12} c="edr-muted">
          {dir}
        </Text>
      )}
    </Group>
  );
}

const PAYMENT_COLORS: Record<string, string> = {
  PAID: "green",
  PENDING: "gray",
  PNR_GENERATED: "blue",
  VERIFICATION_IN_PROGRESS: "yellow",
  FAILED: "red",
};

const PAYMENT_LABELS: Record<string, string> = {
  PAID: "Paid",
  PENDING: "Pending",
  PNR_GENERATED: "PNR generated",
  VERIFICATION_IN_PROGRESS: "Verifying",
  FAILED: "Failed",
};

/** Payment status pill. */
export function PaymentBadge({ status }: { status?: string | null }) {
  if (!status) return <Text fz={13} c="dimmed">—</Text>;
  return (
    <Badge
      variant="light"
      radius="sm"
      color={PAYMENT_COLORS[status] ?? "gray"}
      styles={{ root: { textTransform: "none", fontWeight: 600, letterSpacing: 0 } }}
    >
      {PAYMENT_LABELS[status] ?? status.replace(/_/g, " ")}
    </Badge>
  );
}

/** Whether a booking is assigned to a train yet (scheduling progress). */
export function SchedulingCell({ booking }: { booking: BookingLike & { trainScheduleId?: string | null; schedulingStatus?: string } }) {
  const assigned = !!booking.trainScheduleId;
  const label = assigned
    ? "Assigned"
    : booking.schedulingStatus === "HOLDING"
      ? "Holding"
      : booking.schedulingStatus === "ELIGIBLE"
        ? "Eligible"
        : "Not scheduled";
  return (
    <Badge
      variant="dot"
      radius="sm"
      color={assigned ? "green" : "gray"}
      styles={{ root: { textTransform: "none", fontWeight: 600, letterSpacing: 0 } }}
    >
      {label}
    </Badge>
  );
}
