import { BaseEntity } from '@edr/api-common';
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { ContainerType } from '../../rule-engine/entities/container-type.entity';
import { BookingOrder } from './booking-order.entity';

/**
 * One drawn-down quantity line of an order. For CONTAINER contracts there is one
 * line per container type (matching the contract's pools); for BULK/BREAK_BULK a
 * single line with a null containerTypeId carries the tons/items.
 */
@Entity({ schema: 'freight', name: 'booking_order_lines' })
export class BookingOrderLine extends BaseEntity {
  @Column({ name: 'order_id', type: 'uuid' })
  orderId!: string;

  @ManyToOne(() => BookingOrder, (order) => order.lines, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'order_id' })
  order?: BookingOrder;

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

  @ManyToOne(() => ContainerType, { nullable: true })
  @JoinColumn({ name: 'container_type_id' })
  containerType?: ContainerType | null;

  /** Containers (count), tons, or items depending on the contract's freight/UoM. */
  @Column({ name: 'quantity', type: 'numeric', precision: 12, scale: 3 })
  quantity!: number;

  /**
   * How much of this line is hazardous / refrigerated, entered per order by the
   * customer when they toggle the flag. Drives the HAZARD_SURCHARGE /
   * REEFER_SURCHARGE rates on the spawned child booking. Both ≤ quantity.
   */
  @Column({ name: 'hazardous_quantity', type: 'numeric', precision: 12, scale: 3, default: 0 })
  hazardousQuantity!: number;

  @Column({ name: 'reefer_quantity', type: 'numeric', precision: 12, scale: 3, default: 0 })
  reeferQuantity!: number;
}
