import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { Contract } from './contract.entity';

/**
 * One pre-booking clearance round on a contract (Path B). GENERAL contracts run
 * many cycles; ONE_TIME uses cycle_number = 1. The booking GL creates is linked
 * back via bookingId. See §5.16.
 */
@Entity({ schema: 'freight', name: 'contract_clearance_cycles' })
@Index(['contractId'])
export class ContractClearanceCycle extends BaseEntity {
  @Column({ name: 'contract_id', type: 'uuid' })
  contractId!: string;

  @ManyToOne(() => Contract, (c) => c.clearanceCycles, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'contract_id' })
  contract?: Contract;

  @Column({ name: 'cycle_number', type: 'int' })
  cycleNumber!: number;

  @Column({ name: 'status', type: 'varchar', length: 40, default: 'AWAITING_DOCUMENTS' })
  status!: string;

  @Column({ name: 'booking_id', type: 'uuid', nullable: true })
  bookingId?: string | null;

  @Column({ name: 'started_at', type: 'timestamptz', default: () => 'now()' })
  startedAt!: Date;

  @Column({ name: 'clearance_ready_at', type: 'timestamptz', nullable: true })
  clearanceReadyAt?: Date | null;

  @Column({ name: 'completed_at', type: 'timestamptz', nullable: true })
  completedAt?: Date | null;

  /** ET GL toggle: whether customer must pay duty/tax before DO collection (import). */
  @Column({ name: 'duty_required', type: 'boolean', nullable: true })
  dutyRequired?: boolean | null;

  /** Export RO vessel departure date (Path B export). */
  @Column({ name: 'vessel_departure_date', type: 'date', nullable: true })
  vesselDepartureDate?: string | null;

  /** Import DO: when the vessel arrived in Djibouti. Required on DO upload. */
  @Column({ name: 'vessel_arrival_date', type: 'date', nullable: true })
  vesselArrivalDate?: string | null;

  /** Import DO: when GL Djibouti collected the DO. Required on DO upload. */
  @Column({ name: 'do_collected_date', type: 'date', nullable: true })
  doCollectedDate?: string | null;

  /**
   * Transit-assignee handshake that runs BEFORE the customs declaration: GL
   * Ethiopia asks Djibouti for the officer who will handle the shipment in
   * transit, and Djibouti answers with a name. The declaration step stays shut
   * until `transitAssigneeName` is set; Djibouti may overwrite it later
   * (reassignment) and the newer name simply wins.
   */
  @Column({ name: 'transit_assignee_requested_at', type: 'timestamptz', nullable: true })
  transitAssigneeRequestedAt?: Date | null;

  @Column({ name: 'transit_assignee_requested_by_user_id', type: 'uuid', nullable: true })
  transitAssigneeRequestedByUserId?: string | null;

  /** What GL Ethiopia asked for — shown on the Djibouti queue. */
  @Column({ name: 'transit_assignee_request_note', type: 'text', nullable: true })
  transitAssigneeRequestNote?: string | null;

  /** The officer Djibouti named — free text, no user directory to bind to. */
  @Column({ name: 'transit_assignee_name', type: 'text', nullable: true })
  transitAssigneeName?: string | null;

  @Column({ name: 'transit_assignee_assigned_at', type: 'timestamptz', nullable: true })
  transitAssigneeAssignedAt?: Date | null;

  @Column({ name: 'transit_assignee_assigned_by_user_id', type: 'uuid', nullable: true })
  transitAssigneeAssignedByUserId?: string | null;

  @Column({ name: 'ro_amendment_requested_at', type: 'timestamptz', nullable: true })
  roAmendmentRequestedAt?: Date | null;

  @Column({ name: 'ro_hold_reason', type: 'text', nullable: true })
  roHoldReason?: string | null;

  @Column({ name: 'current_phase', type: 'varchar', length: 40, nullable: true })
  currentPhase?: string | null;

  /** ET GL confirms import pre-clearance complete — unlocks Djibouti DO upload. */
  @Column({ name: 'pre_clearance_finalized_at', type: 'timestamptz', nullable: true })
  preClearanceFinalizedAt?: Date | null;
}
