// apps/edr-freight-api/src/modules/cargoes/entities/cargo.entity.ts
import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '@edr/api-common';
import { Container } from '../../container-management/entities/container.entity';

@Entity({ name: 'cargoes', schema: 'freight' })
export class Cargo extends BaseEntity {
  @Column({ unique: true, name: 'cargo_reference' })
  cargoReference!: string;

  @Column({ name: 'shipment_id', type: 'uuid' })
  shipmentId!: string;

  @Column({ name: 'container_id', type: 'uuid' })
  containerId!: string;

  @Column({ name: 'cargo_type_id', type: 'uuid', nullable: true })
  cargoTypeId!: string | null; // optional link to cargo_types table

  @Column({ type: 'text', nullable: true })
  description!: string | null;

  @Column({ type: 'decimal', precision: 12, scale: 3 })
  quantity!: number;

  @Column({ type: 'decimal', precision: 10, scale: 2 })
  weight!: number; // kg

  @Column({ type: 'decimal', precision: 10, scale: 2, nullable: true })
  volume!: number | null; // m³

  @Column({ type: 'varchar', default: 'PENDING' })
  status!: string; // PENDING, LOADED, IN_TRANSIT, DELIVERED, UNLOADED

  @Column({ name: 'loaded_at', type: 'timestamp', nullable: true })
  loadedAt!: Date | null;

  @Column({ name: 'unloaded_at', type: 'timestamp', nullable: true })
  unloadedAt!: Date | null;

  // Relationship to Container
  @ManyToOne(() => Container, (container) => container.cargoes, { onDelete: 'RESTRICT' })
  @JoinColumn({ name: 'container_id' })
  container!: Container;
}