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

import { LastMileVehicleAssignment } from './last-mile-vehicle-assignment.entity';

/**
 * A container riding a specific EDR last-mile truck. A truck carries 1x40ft OR
 * 2x20ft, so the assignment needs more than the single legacy `container_number`
 * scalar. Mirrors the self-haul `customer_truck_containers` child table.
 */
@Entity({ schema: 'freight', name: 'last_mile_vehicle_containers' })
@Index(['assignmentId'])
export class LastMileVehicleContainer extends BaseEntity {
  @Column({ name: 'assignment_id', type: 'uuid' })
  assignmentId!: string;

  @ManyToOne(() => LastMileVehicleAssignment, (a) => a.containers, {
    nullable: false,
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'assignment_id' })
  assignment?: LastMileVehicleAssignment;

  /** Denormalised for the "one container, one truck per delivery" unique index. */
  @Column({ name: 'last_mile_id', type: 'uuid' })
  lastMileId!: string;

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