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

import { Locomotive } from '../../locomotives/entities/locomotive.entity';
import { TrainSet } from './train-set.entity';

/**
 * Link row joining a train set to one of its locomotives. A train set must be
 * pulled by at least two locomotives (front + back); `sequenceNo` is a plain
 * order index — no front/rear semantics are modelled yet.
 */
@Entity({ schema: 'freight', name: 'train_set_locomotives' })
@Index(['trainSetId', 'locomotiveId'], { unique: true })
export class TrainSetLocomotive extends BaseEntity {
  @Column({ name: 'train_set_id', type: 'uuid' })
  trainSetId!: string;

  @ManyToOne(() => TrainSet, (trainSet) => trainSet.locomotives, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'train_set_id' })
  trainSet?: TrainSet;

  @Column({ name: 'locomotive_id', type: 'uuid' })
  locomotiveId!: string;

  @ManyToOne(() => Locomotive)
  @JoinColumn({ name: 'locomotive_id' })
  locomotive?: Locomotive;

  @Column({ name: 'sequence_no', type: 'int', default: 0 })
  sequenceNo!: number;
}
