import { bookings } from "../bookings/bookings.mock";

export type ConsignmentStatus =
  | "Pending"
  | "In Warehouse"
  | "In Transit"
  | "Delivered"
  | "Returned";

export type ConsignmentCargo =
  | "Containerized"
  | "Bulk"
  | "Liquid"
  | "Refrigerated"
  | "Hazardous"
  | "General";

export interface Consignment {
  id: number;
  trackingNumber: string;
  bookingId: number;
  bookingReference: string;
  customer: string;
  originStation: string;
  destinationStation: string;
  cargoType: ConsignmentCargo;
  description: string;
  weightKg: number;
  volumeM3: number;
  pieces: number;
  hazardous: boolean;
  specialHandling: string;
  status: ConsignmentStatus;
  createdAt: string;
  estimatedDelivery: string;
}

const cargoTypes: ConsignmentCargo[] = [
  "Containerized",
  "Bulk",
  "Liquid",
  "Refrigerated",
  "Hazardous",
  "General",
];

const statuses: ConsignmentStatus[] = [
  "Pending",
  "In Warehouse",
  "In Transit",
  "Delivered",
  "Returned",
];

const descriptions = [
  "Coffee beans, sealed sacks",
  "Construction rebar bundles",
  "Industrial machinery parts",
  "Textile rolls",
  "Bottled mineral water",
  "Refined cooking oil",
  "Frozen meat products",
  "Pharmaceutical supplies",
  "Cement bags",
  "Electronics consignment",
  "Automotive spare parts",
  "Packaged food goods",
];

const handlings = [
  "Keep dry. Stack max 3 high.",
  "Fragile — handle with care.",
  "Temperature-controlled (2–8°C).",
  "Oversized load — escort required.",
  "Hazmat class 3 — segregate.",
  "Standard handling.",
];

export const consignments: Consignment[] = Array.from({ length: 22 }, (_, i) => {
  const booking = bookings[i % bookings.length] as (typeof bookings)[number];
  const id = i + 1;
  const created = new Date(2026, 4, 1 + (i % 14));
  const eta = new Date(created);
  eta.setDate(eta.getDate() + 5 + (i % 5));
  const cargoType = cargoTypes[i % cargoTypes.length] as ConsignmentCargo;
  return {
    id,
    trackingNumber: `CGM-${String(id).padStart(4, "0")}`,
    bookingId: booking.id,
    bookingReference: booking.reference,
    customer: booking.customer,
    originStation: booking.originStation,
    destinationStation: booking.destinationStation,
    cargoType,
    description: descriptions[i % descriptions.length] as string,
    weightKg: 500 + (i * 137) % 9500,
    volumeM3: Math.round(((i % 10) + 2) * 10) / 10,
    pieces: (i % 12) + 1,
    hazardous: cargoType === "Hazardous",
    specialHandling: handlings[i % handlings.length] as string,
    status: statuses[i % statuses.length] as ConsignmentStatus,
    createdAt: created.toISOString().slice(0, 10),
    estimatedDelivery: eta.toISOString().slice(0, 10),
  };
});

export function getConsignmentById(
  id: number | string,
): Consignment | undefined {
  const numericId = typeof id === "string" ? Number(id) : id;
  return consignments.find((c) => c.id === numericId);
}
