/**
 * Shared contracts for live booking-window pushes.
 *
 * The freight API emits one event whenever a schedule's booking-window state
 * changes (phase transition, open/close, cycle reopen). Portal home and the
 * backoffice GL windows section subscribe and refresh instantly instead of
 * waiting for their poll interval.
 */

/** Booking-window lifecycle phase persisted on a train schedule. */
export type BookingWindowPhase =
  | "PRE_WINDOW"
  | "OPEN"
  | "DOC_REVIEW"
  | "PAYMENT"
  | "CLOSED_FOR_DAY"
  | "DONE";

/** Payload pushed on every booking-window state change. */
export interface BookingWindowPhaseEvent {
  scheduleId: string;
  originYardId: string;
  destinationYardId: string;
  direction: string | null;
  phase: BookingWindowPhase;
  bookingWindowStatus: string | null;
  bookingCycleNo: number;
  windowOpensAt: string | null;
  windowClosesAt: string | null;
  docReviewEndsAt: string | null;
  paymentPhaseEndsAt: string | null;
  scheduledDepartureDate: string | null;
}

/**
 * Payload pushed whenever server-side state behind a schedule's batch board
 * changes outside a phase transition (payment settled, allocation run, batch
 * fill, reservation expiry, doc review, …). Carries no board data — clients
 * refetch the views they hold; `reason` exists for debugging/telemetry only.
 */
export interface BatchBoardChangedEvent {
  scheduleId: string;
  reason: string;
  at: string;
}

/** Socket.io event names pushed server → client on the booking-windows namespace. */
export const BOOKING_WINDOW_WS_EVENTS = {
  PHASE: "booking-window:phase",
  BATCH_CHANGED: "batch-board:changed",
} as const;

/** Socket.io namespace the booking-window gateway listens on. */
export const BOOKING_WINDOW_WS_NAMESPACE = "booking-windows";
