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

import { Booking } from '../../bookings/entities/booking.entity';
import { TrainSchedule } from './train-schedule.entity';

export const TRAIN_SCHEDULE_BOOKING_LOADING_STATUSES = [
  LoadingStatus.Unloaded,
  LoadingStatus.Loaded,
] as const;

@Entity({ schema: 'freight', name: 'train_schedule_bookings' })
@Index(['trainScheduleId', 'bookingId'], { unique: true })
@Index(['bookingId'], { unique: true })
export class TrainScheduleBooking extends BaseEntity {
  @Column({ name: 'train_schedule_id', type: 'uuid' })
  trainScheduleId!: string;

  @ManyToOne(() => TrainSchedule, (trainSchedule) => trainSchedule.scheduleBookings, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'train_schedule_id' })
  trainSchedule?: TrainSchedule;

  @Column({ name: 'booking_id', type: 'uuid' })
  bookingId!: string;

  @ManyToOne(() => Booking)
  @JoinColumn({ name: 'booking_id' })
  booking?: Booking;

  @Column({ name: 'loading_status', type: 'varchar', length: 20, default: 'UNLOADED' })
  loadingStatus!: string;
}
