/** Mirrors API BOOKING_STATUSES from edr-freight-api booking.entity */
export const BOOKING_STATUSES = [
  "DRAFT",
  "SUBMITTED",
  "CHANGES_REQUESTED",
  "PENDING_APPROVAL",
  "APPROVED_PENDING_SIGNATURE",
  "APPROVED",
  "READY_FOR_ASSIGNMENT",
  "WAGON_ASSIGNED",
  "INVOICED",
  "CONTRACT_READY",
  "SIGNED_CUSTOMER",
  "FULLY_EXECUTED",
  "PNR_GENERATED",
  "PAYMENT_VERIFICATION_IN_PROGRESS",
  "PAID",
  "IN_TRANSIT",
  "COMPLETED",
  "REJECTED",
  "CANCELLED",
  "PENDING_CONSOLIDATION",
  "CONSOLIDATED",
] 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;
}

export interface BookingContainerLine {
  id: string;
  containerTypeId: string;
  quantity: number;
  vgmPerUnitTons: number;
  containerType?: {
    id: string;
    code?: string;
    label?: string;
    sizeFt?: number;
    isReefer?: boolean;
  };
}

export interface BookingApprovalStep {
  id: string;
  stepOrder: number;
  requiredRole: string;
  blocksRole?: string | null;
  status: "PENDING" | "APPROVED" | "REJECTED" | "SKIPPED";
  actionedAt?: string | null;
  remarks?: string | null;
}

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;
  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;
  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;
  contractSummary?: string | null;
  latestChangeRequestNote?: string | null;
  nextStep?: BookingNextStep | null;
  paymentReceipt?: InAppPaymentReceipt;
  createdAt: string;
  updatedAt: string;
  // customer?: BookingNamedRef & { companyName?: string };
  company?: BookingNamedRef & Partial<BookingCompany>;
  originYard?: BookingNamedRef;
  destinationYard?: BookingNamedRef;
  serviceType?: BookingNamedRef & { code?: string; priorityBonusPoints?: number };
  cargoType?: BookingNamedRef;
  shippingLine?: BookingNamedRef;
  bookingContainers?: BookingContainerLine[];
  approvalSteps?: BookingApprovalStep[];
  reviewNotes?: BookingReviewNote[];
  files?: BookingFile[];
  cargoModifiers?: Array<{
    id: string;
    calculatedAmount: number;
    triggerValue?: number | null;
  }>;
}

export interface BookingListRow {
  id: string;
  reference: string;
  customerLabel: string;
  approvalSteps?: BookingApprovalStep[];
  status: BookingStatus;
  scheduledDate: string;
  totalAmount: number;
  paymentCurrency: string;
  paymentStatus: string;
  tradeDirection: string;
  freightType: string;
  originLabel: string;
  destinationLabel: string;
  priorityScore: number;
  schedulingStatus?: string;
  serviceTypeLabel?: string;
  serviceTypeBonus?: number;
  trainScheduleId?: string | null;
  isGovernment?: boolean;
  governmentInstitution?: string | null;
  consolidationPartnerId?: string | null;
  consolidationPartnerReference?: string | null;
  createdAt: string;
}
