import type { ContractDocPhase } from '@edr/types';

/** Shared clearance metadata for contract cycles and per-booking GENERAL clearance. */
export interface ClearanceMetaState {
  dutyRequired?: boolean | null;
  vesselDepartureDate?: string | null;
  roAmendmentRequestedAt?: Date | null;
  roHoldReason?: string | null;
  currentPhase?: ContractDocPhase | string | null;
  preClearanceFinalizedAt?: Date | null;
}

export type ClearanceScope =
  | { kind: 'contract'; contractId: string }
  | { kind: 'booking'; bookingId: string };

export function metaFromBooking(booking: {
  dutyRequired?: boolean | null;
  vesselDepartureDate?: string | null;
  roAmendmentRequestedAt?: Date | null;
  roHoldReason?: string | null;
  clearanceCurrentPhase?: string | null;
  preClearanceFinalizedAt?: Date | null;
}): ClearanceMetaState {
  return {
    dutyRequired: booking.dutyRequired ?? null,
    vesselDepartureDate: booking.vesselDepartureDate ?? null,
    roAmendmentRequestedAt: booking.roAmendmentRequestedAt ?? null,
    roHoldReason: booking.roHoldReason ?? null,
    currentPhase: booking.clearanceCurrentPhase ?? null,
    preClearanceFinalizedAt: booking.preClearanceFinalizedAt ?? null,
  };
}
