// apps/edr-freight-api/src/modules/wagons/entities/wagon.entity.ts
import { WagonStatus } from '@edr/types';
import { Entity, Column, ManyToOne, OneToMany, JoinColumn, Index } from 'typeorm';
import { BaseEntity } from '@edr/api-common';
import { Train } from '../../trains/entities/train.entity';
import { TrainSchedule } from '../../train-schedules/entities/train-schedule.entity';
import { TrainSetWagon } from '../../train-sets/entities/train-set-wagon.entity';
import { Container } from '../../container-management/entities/container.entity';
import { Yard } from '../../rule-engine/entities/yard.entity';

export const WAGON_STATUSES = [
  WagonStatus.Available,
  WagonStatus.Assigned,
  WagonStatus.Maintenance,
  WagonStatus.Retired,
] as const;

export type WagonStatusType = (typeof WAGON_STATUSES)[number];

@Entity({ name: 'wagons', schema: 'freight' })
@Index(['currentYardId'])
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', length: 20, default: WagonStatus.Available })
  status!: WagonStatusType;

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

  @ManyToOne(() => Yard, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'current_yard_id' })
  currentYard?: Yard | null;

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

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

  @ManyToOne(() => TrainSetWagon, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'train_set_wagon_id' })
  trainSetWagon?: TrainSetWagon | null;

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

  @ManyToOne(() => TrainSchedule, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'current_train_schedule_id' })
  currentTrainSchedule?: TrainSchedule | null;

  /** Fleet master consist grouping — separate from operational train_schedules. */
  @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[];
}