// apps/edr-freight-api/src/modules/wagons/entities/wagon.entity.ts
import { Entity, Column, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { BaseEntity } from '@edr/api-common';
import { Train } from '../../trains/entities/train.entity';
import { Container } from '../../container-management/entities/container.entity';

@Entity({ name: 'wagons', schema: 'freight' })
export class Wagon extends BaseEntity {
  @Column({ unique: true, name: 'wagon_number' })
  wagonNumber!: string;

  @Column({ name: 'wagon_type_id', type: 'uuid' })
  wagonTypeId!: string;

  @Column({ name: 'train_id', type: 'uuid', nullable: true })
  trainId!: string | null;

  @Column({ name: 'sequence_number', type: 'int', nullable: true })
  sequenceNumber!: number | null;

  @Column({ name: 'tare_weight', type: 'decimal', precision: 10, scale: 2 })
  tareWeight!: number;

  @Column({ name: 'max_payload_weight', type: 'decimal', precision: 10, scale: 2 })
  maxPayloadWeight!: number;

  @Column({ type: 'varchar', default: 'AVAILABLE' })
  status!: string; // AVAILABLE, ASSIGNED, MAINTENANCE, RETIRED

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

  // Relationship to Train
  @ManyToOne(() => Train, (train) => train.wagons, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'train_id' })
  train!: Train | null;

  // Relationship to Container
  @OneToMany(() => Container, (container) => container.wagon)
  containers!: Container[];
}