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

/**
 * One physical container under a booking_container line — its number, seal, and
 * per-unit VGM. Entered at booking time (by the customer in Path A or by GL ET
 * in Path B). See §5.10.
 */
@Entity({ schema: 'freight', name: 'booking_container_units' })
@Index(['bookingContainerId'])
export class BookingContainerUnit extends BaseEntity {
  @Column({ name: 'booking_container_id', type: 'uuid' })
  bookingContainerId!: string;

  @ManyToOne(() => BookingContainer, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'booking_container_id' })
  bookingContainer?: BookingContainer;

  @Column({ name: 'container_number', type: 'varchar', length: 64 })
  containerNumber!: string;

  @Column({ name: 'seal_number', type: 'varchar', length: 64, nullable: true })
  sealNumber?: string | null;

  @Column({ name: 'vgm_tons', type: 'numeric', precision: 10, scale: 3 })
  vgmTons!: number;

  @Column({ name: 'is_hazardous', type: 'boolean', default: false })
  isHazardous!: boolean;

  @Column({ name: 'is_reefer', type: 'boolean', default: false })
  isReefer!: boolean;

  /** This container ships back empty after unloading (equipment return). */
  @Column({ name: 'is_return', type: 'boolean', default: false })
  isReturn!: boolean;

  @Column({ name: 'sort_order', type: 'smallint', default: 0 })
  sortOrder!: number;

  /** Whether this container has been received into the port (auto-set when its
   *  self-haul truck arrives). */
  @Column({ name: 'received_to_port', type: 'boolean', default: false })
  receivedToPort!: boolean;

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

  /** The GRN this container was received under (assigned when staff confirm the
   *  Goods Received Note for a batch of received containers). */
  @Column({ name: 'grn_number', type: 'varchar', length: 100, nullable: true })
  grnNumber?: string | null;
}
