import type { Freight } from "@edr/types";

export interface ShipmentListRow {
  id: string;
  reference: string;
  contractId: string;
  contractReference: string;
  scheduledDate?: string | null;
  summary: string;
  status: Freight.BookingRequestStatus;
  createdBookingId?: string | null;
  /** When the customer submitted the request — the queue's default sort key. */
  createdAt?: string | null;
  customerName?: string | null;
  freightKind?: "CONTAINER" | "BULK";
  hazardous?: boolean;
  reefer?: boolean;
}

export type ShipmentRowAction =
  | {
      kind: "navigate";
      label: string;
      to: (row: ShipmentListRow) => string;
      variant: "filled" | "light" | "default";
    }
  | {
      kind: "reject";
      label: string;
      variant: "light";
    };

/** Primary staff action for a shipment request list row. */
export function getShipmentStaffRowAction(
  row: Pick<
    ShipmentListRow,
    "status" | "contractId" | "id" | "createdBookingId"
  >,
): ShipmentRowAction {
  if (row.status === "PENDING") {
    return {
      kind: "navigate",
      label: "Accept",
      to: (r) =>
        `/dashboard/contracts/${r.contractId}/create-booking?requestId=${r.id}`,
      variant: "filled",
    };
  }
  if (row.status === "ACCEPTED" && row.createdBookingId) {
    return {
      kind: "navigate",
      label: "View clearance",
      to: (r) => `/dashboard/clearance/${r.createdBookingId}`,
      variant: "light",
    };
  }
  return {
    kind: "navigate",
    label: "Review",
    to: (r) => `/dashboard/shipment-requests/${r.id}`,
    variant: "default",
  };
}

export function getShipmentRejectAction(
  row: Pick<ShipmentListRow, "status">,
): ShipmentRowAction | null {
  if (row.status !== "PENDING") return null;
  return { kind: "reject", label: "Reject", variant: "light" };
}
