/** Mirrors API BOOKING_STATUSES from edr-freight-api booking.entity */
export const BOOKING_STATUSES = [
  "DRAFT",
  "SUBMITTED",
  "PRICE_CHANGED_PENDING_CONFIRM",
  "CHANGES_REQUESTED",
  "PENDING_APPROVAL",
  "APPROVED_PENDING_SIGNATURE",
  "APPROVED",
  "READY_FOR_ASSIGNMENT",
  "WAGON_ASSIGNED",
  "INVOICED",
  "CONTRACT_READY",
  "SIGNED_CUSTOMER",
  "FULLY_EXECUTED",
  "SELECTED_FOR_BATCH",
  "EXPIRED",
  "PNR_GENERATED",
  "PAYMENT_VERIFICATION_IN_PROGRESS",
  "PAID",
  "IN_TRANSIT",
  "ARRIVED",
  "COMPLETED",
  "REJECTED",
  "CANCELLED",
  "PENDING_CONSOLIDATION",
  "CONSOLIDATED",
  "CONTRACT_ACTIVE",
  "CONTRACT_CLOSED",
  // Post counter-sign document-clearance gate.
  "AWAITING_DOCUMENTS",
  "DOCUMENTS_UNDER_REVIEW",
  "CLEARANCE_READY",
  "ROAD_DISPATCH_PENDING",
  "OPERATION_REQUESTED",
  // Operations review gate.
  "OPERATION_REQUEST_PENDING",
  "OPERATION_CHANGES_REQUESTED",
  "OPERATION_PRICE_PENDING_CONFIRM",
] as const;

export type BookingStatus = (typeof BOOKING_STATUSES)[number];

export interface BookingNamedRef {
  id: string;
  name?: string;
  code?: string;
  label?: string;
  companyName?: string;
}

/** Full company record the booking response joins in (subset used by the UI). */
export interface BookingCompany {
  id: string;
  name?: string;
  type?: string;
  status?: string;
  tin?: string | null;
  vatNumber?: string | null;
  businessLicense?: string | null;
  country?: string | null;
  address?: string | null;
  phone?: string | null;
  email?: string | null;
  contactPersonName?: string | null;
  contactPersonPhone?: string | null;
  generalManagerName?: string | null;
  generalManagerEmail?: string | null;
  generalManagerPhone?: string | null;
  website?: string | null;
}

/** One physical container under a line — its own number + verified gross mass. */
export interface BookingContainerUnit {
  id: string;
  containerNumber: string;
  sealNumber?: string | null;
  vgmTons: number;
  isHazardous?: boolean;
  isReefer?: boolean;
  /** This container ships back empty after unloading (equipment return). */
  isReturn?: boolean;
  sortOrder?: number;
}

export interface BookingContainerLine {
  id: string;
  containerTypeId: string;
  /** Line-level number — often a "TBD-…" placeholder; real numbers live in units. */
  containerNumber?: string | null;
  quantity: number;
  vgmPerUnitTons: number;
  containerType?: {
    id: string;
    code?: string;
    label?: string;
    sizeFt?: number;
  };
  /** Per-physical-container rows (number + weight). Empty when not captured. */
  units?: BookingContainerUnit[];
}

export interface BookingNextStep {
  action: string;
  description: string;
  requiredRole?: string;
}

export interface InAppPaymentReceipt {
  success: boolean;
  provider: string;
  providerRef: string;
  amount: number;
  currency: string;
  paidAt: string;
}

export interface BookingReviewNote {
  id: string;
  note: string;
  type: string;
  createdAt: string;
}

export interface BookingFile {
  id: string;
  name: string;
  mimeType?: string;
  code?: string;
  url?: string;
  size?: number;
}

export interface BookingDetail {
  id: string;
  reference: string;
  // customerId: string;
  companyId?: string | null;
  isGovernment?: boolean;
  governmentInstitution?: string | null;
  status: BookingStatus;
  /** ONE_TIME shipment vs an umbrella GENERAL_CONTRACT drawn down by orders. */
  bookingType?: "ONE_TIME" | "GENERAL_CONTRACT";
  /** General contracts only: when the ordering window closes. */
  expiresAt?: string | null;
  scheduledDate: string;
  totalAmount: number;
  adjustedTotalAmount?: number | null;
  adjustedByStaffId?: string | null;
  adjustedAt?: string | null;
  adjustmentReason?: string | null;
  /** Contract validity window set by the backoffice when accepting. */
  contractValidityDays?: number | null;
  contractValidFrom?: string | null;
  contractValidUntil?: string | null;
  pricingBreakdown?: {
    currency: string;
    totalAmount: number;
    lineItems: Array<{
      code: string;
      description: string;
      amount: number;
      currency: string;
    }>;
  } | null;
  paymentStatus: string;
  paymentCurrency: string;
  contractType: string;
  freightType: "CONTAINER" | "BULK";
  tradeDirection: string;
  /** What the containers carry / bulk commodity label — entered at booking time. */
  cargoFreeText?: string | null;
  cargoTotalWeightVgm: number;
  isHazardous: boolean;
  consolidationPartnerId?: string | null;
  consolidationPartner?: BookingNamedRef & { reference?: string } | null;
  priorityScore: number;
  schedulingStatus?: string;
  holdExpiresAt?: string | null;
  holdStartedAt?: string | null;
  wagonsRequired?: number | null;
  scheduledAt?: string | null;
  trainScheduleId?: string | null;
  pnrCode?: string | null;
  firstMilePickupAddress?: string | null;
  lastMileDeliveryAddress?: string | null;
  equipmentReturn?: string;
  customsClearingEnabled?: boolean;
  customsClearingAgent?: string | null;
  contractKind?: "ONE_TIME" | "GENERAL" | null;
  contractId?: string | null;
  /** Reference of the contract this booking was created under (list column + search). */
  contractReference?: string | null;
  contractSummary?: string | null;
  latestChangeRequestNote?: string | null;
  nextStep?: BookingNextStep | null;
  paymentReceipt?: InAppPaymentReceipt;
  /** Phased-clearance fields the ET/DJ queue rows surface (GENERAL customs bookings). */
  clearanceCurrentPhase?: string | null;
  roHoldReason?: string | null;
  roAmendmentRequestedAt?: string | null;
  preClearanceFinalizedAt?: string | null;
  createdAt: string;
  updatedAt: string;
  // customer?: BookingNamedRef & { companyName?: string };
  company?: BookingNamedRef & Partial<BookingCompany>;
  originYard?: BookingNamedRef;
  destinationYard?: BookingNamedRef;
  serviceType?: BookingNamedRef & { code?: string; includesCustoms?: boolean; includesFirstMile?: boolean; includesLastMile?: boolean };
  cargoType?: BookingNamedRef;
  shippingLine?: BookingNamedRef;
  bookingContainers?: BookingContainerLine[];
  reviewNotes?: BookingReviewNote[];
  files?: BookingFile[];
  cargoModifiers?: Array<{
    id: string;
    calculatedAmount: number;
    triggerValue?: number | null;
  }>;
}

export interface BookingListRow {
  id: string;
  reference: string;
  contractReference?: string | null;
  /** Needed to link the reference to the contract's detail page. */
  contractId?: string | null;
  customerLabel: string;
  status: BookingStatus;
  scheduledDate: string;
  totalAmount: number;
  paymentCurrency: string;
  paymentStatus: string;
  tradeDirection: string;
  freightType: string;
  originLabel: string;
  destinationLabel: string;
  priorityScore: number;
  schedulingStatus?: string;
  serviceTypeLabel?: string;
  trainScheduleId?: string | null;
  isGovernment?: boolean;
  governmentInstitution?: string | null;
  consolidationPartnerId?: string | null;
  consolidationPartnerReference?: string | null;
  customsClearingEnabled?: boolean;
  /**
   * Derived booking kind for the list "Type" column. Mirrors the server's
   * bookingType filter: bookings under a GENERAL contract are general,
   * everything else is one-time.
   */
  bookingKind?: "ONE_TIME" | "GENERAL_CONTRACT";
  createdAt: string;
}
