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

import { BookingContainer } from '../../bookings/entities/booking-container.entity';
import { Container } from '../../container-management/entities/container.entity';
import { ContainerType } from '../../rule-engine/entities/container-type.entity';
import { WagonBookingAllocation } from './wagon-booking-allocation.entity';

@Entity({ schema: 'freight', name: 'wagon_allocation_container_items' })
@Index(['wagonBookingAllocationId'])
export class WagonAllocationContainerItem extends BaseEntity {
  @Column({ name: 'wagon_booking_allocation_id', type: 'uuid' })
  wagonBookingAllocationId!: string;

  @ManyToOne(() => WagonBookingAllocation, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'wagon_booking_allocation_id' })
  allocation?: WagonBookingAllocation;

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

  @ManyToOne(() => BookingContainer, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'booking_container_id' })
  bookingContainer?: BookingContainer | null;

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

  @ManyToOne(() => Container, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'container_id' })
  container?: Container | null;

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

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

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

  @Column({ name: 'position_on_wagon', type: 'smallint', nullable: true })
  positionOnWagon?: number | null;

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

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

  @Column({ name: 'gross_weight_tons', type: 'numeric', precision: 10, scale: 3, nullable: true })
  grossWeightTons?: number | null;
}
