// apps/edr-freight-api/src/modules/trains/entities/train.entity.ts
import { BaseEntity } from '@edr/api-common';
import { Freight } from '@edr/types';
import { Column, Entity, OneToMany } from 'typeorm';
import { Wagon } from '../../wagons/entities/wagon.entity';

/**
 * Fleet master data — named wagon consist in inventory (POST /trains).
 * Operational departures use train_schedules + locomotives; scheduling never creates trains rows.
 */
@Entity({ schema: 'freight', name: 'trains' })
export class Train extends BaseEntity {
  // --- existing fields (keep for backward compatibility) ---
  @Column({ name: 'code', type: 'varchar', length: 32, unique: true })
  code!: string;

  @Column({ name: 'capacity_tons', type: 'numeric', precision: 10, scale: 2 })
  capacityTons!: number;

  @Column({
    name: 'status',
    type: 'enum',
    enum: Freight.TrainStatus,
    default: Freight.TrainStatus.Available,
  })
  status!: Freight.TrainStatus;

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

  // --- new required fields ---
  @Column({ name: 'train_number', type: 'varchar', length: 20, unique: true, nullable: true })
  trainNumber?: string;

  @Column({ name: 'train_name', type: 'varchar', length: 100, nullable: true })
  trainName?: string;

  @Column({ name: 'route_id', type: 'uuid', nullable: true })
  routeId?: string;

  @Column({ name: 'origin_station_id', type: 'uuid', nullable: true })
  originStationId?: string;

  @Column({ name: 'destination_station_id', type: 'uuid', nullable: true })
  destinationStationId?: string;

  @Column({ name: 'departure_time', type: 'timestamp', nullable: true })
  departureTime?: Date;

  @Column({ name: 'arrival_time', type: 'timestamp', nullable: true })
  arrivalTime?: Date;

  @Column({ name: 'locomotive_number', type: 'varchar', length: 50, nullable: true })
  locomotiveNumber?: string;

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

  // --- relationships ---
   @OneToMany(() => Wagon, (wagon) => wagon.train)
  wagons!: Wagon[];  // fixed typo: was 'wagens'
}