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

export const FACILITY_HANDLING_EVENT_TYPES = ['LOAD', 'UNLOAD'] as const;
export type FacilityHandlingEventType = (typeof FACILITY_HANDLING_EVENT_TYPES)[number];

/**
 * Cargo loaded onto or unloaded off a train at a yard's facility, and the GRN
 * raised for it.
 *
 * This exists because warehouse_inventory can't do the job: its
 * warehouse/yard/zone are NOT NULL, so a facility that only has equipment and no
 * warehouse (Sebeta, Modjo, Adama, Dire Dawa) could never have a row there —
 * yet it still hands cargo over and still needs a GRN.
 *
 * `inventoryId` links to the warehouse record when the facility does store cargo
 * (Indode), which is what makes storage and demurrage accrue there and nowhere
 * else.
 */
@Entity({ schema: 'freight', name: 'facility_handling_events' })
@Index(['bookingId'])
@Index(['yardId'])
export class FacilityHandlingEvent extends BaseEntity {
  @Column({ name: 'booking_id', type: 'uuid' })
  bookingId!: string;

  /** The facility yard where the cargo was handled. */
  @Column({ name: 'yard_id', type: 'uuid' })
  yardId!: string;

  /** The train the cargo came off / went onto. */
  @Column({ name: 'train_schedule_id', type: 'uuid', nullable: true })
  trainScheduleId?: string | null;

  @Column({ name: 'event_type', type: 'varchar', length: 10 })
  eventType!: FacilityHandlingEventType;

  @Column({ name: 'grn_number', type: 'varchar', length: 60, nullable: true })
  grnNumber?: string | null;

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

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

  /** Set only when the facility stores cargo (has_warehouse) — the storage record. */
  @Column({ name: 'inventory_id', type: 'uuid', nullable: true })
  inventoryId?: string | null;

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

  @Column({ name: 'occurred_at', type: 'timestamptz', default: () => 'now()' })
  occurredAt!: Date;
}
