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

import { Booking } from '../../bookings/entities/booking.entity';
import { WarehouseInventory } from './warehouse-inventory.entity';

/**
 * Batch 3 — a record that a warehouse inventory item was physically loaded onto a wagon.
 * The warehouse OWNS this record. It only READS wagon/schedule data from the scheduling
 * domain (via SchedulingReadFacade); it never writes to wagons or train schedules.
 */
@Entity({ schema: 'freight', name: 'warehouse_loadings' })
@Index(['warehouseInventoryId'])
@Index(['bookingId'])
@Index(['wagonId'])
export class WarehouseLoading extends BaseEntity {
  @Column({ name: 'warehouse_inventory_id', type: 'uuid' })
  warehouseInventoryId!: string;

  @ManyToOne(() => WarehouseInventory)
  @JoinColumn({ name: 'warehouse_inventory_id' })
  inventory?: WarehouseInventory;

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

  @ManyToOne(() => Booking, { nullable: true })
  @JoinColumn({ name: 'booking_id' })
  booking?: Booking | null;

  /** Physical wagon the item was loaded onto. References freight.wagons (read-only link). */
  @Column({ name: 'wagon_id', type: 'uuid' })
  wagonId!: string;

  @Column({ name: 'loaded_at', type: 'timestamptz' })
  loadedAt!: Date;

  @Column({ name: 'loaded_by', type: 'varchar', length: 120, nullable: true })
  loadedBy?: string | null;

  @Column({ name: 'loaded_weight', type: 'numeric', precision: 14, scale: 3, nullable: true })
  loadedWeight?: number | null;

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