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

import { Booking } from '../../bookings/entities/booking.entity';
import { CargoType } from '../../rule-engine/entities/cargo-type.entity';
import { WagonBookingAllocation } from './wagon-booking-allocation.entity';

export const BULK_PRICING_UNITS = [
  BulkPricingUnit.PerWagon,
  BulkPricingUnit.PerTon,
  BulkPricingUnit.PerItem,
] as const;

@Entity({ schema: 'freight', name: 'wagon_allocation_bulk_loads' })
@Index(['bookingId'])
export class WagonAllocationBulkLoad extends BaseEntity {
  @Column({ name: 'wagon_booking_allocation_id', type: 'uuid', unique: true })
  wagonBookingAllocationId!: string;

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

  @Column({ name: 'booking_id', type: 'uuid' })
  bookingId!: string;

  @ManyToOne(() => Booking)
  @JoinColumn({ name: 'booking_id' })
  booking?: Booking;

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

  @ManyToOne(() => CargoType, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'cargo_type_id' })
  cargoType?: CargoType | null;

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

  @Column({ name: 'pricing_unit', type: 'varchar', length: 20, default: BulkPricingUnit.PerTon })
  pricingUnit!: string;

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

  @Column({ name: 'weight_tons', type: 'numeric', precision: 10, scale: 3, default: 0 })
  weightTons!: number;

  @Column({ name: 'truck_plate_number', type: 'varchar', length: 32, nullable: true })
  truckPlateNumber?: string | null;
}
