import { BaseEntity } from '@edr/api-common';
import { TrainScheduleStatus as TrainScheduleStatusEnum } from '@edr/types';
import { Column, Entity, Index, JoinColumn, ManyToOne, OneToMany, OneToOne } from 'typeorm';

import { Yard } from '../../rule-engine/entities/yard.entity';
import { Route } from '../../routes/entities/route.entity';
import { TrainSet } from '../../train-sets/entities/train-set.entity';
import { TrainScheduleBooking } from './train-schedule-booking.entity';

export const TRAIN_SCHEDULE_STATUSES = [
  TrainScheduleStatusEnum.Draft,
  TrainScheduleStatusEnum.Scheduled,
  TrainScheduleStatusEnum.Dispatched,
  TrainScheduleStatusEnum.Arrived,
  TrainScheduleStatusEnum.Cancelled,
] as const;

export type TrainScheduleStatus = (typeof TRAIN_SCHEDULE_STATUSES)[number];

@Entity({ schema: 'freight', name: 'train_schedules' })
@Index(['scheduledDepartureDate'])
@Index(['status'])
export class TrainSchedule extends BaseEntity {
  @Column({ name: 'train_set_id', type: 'uuid', unique: true })
  trainSetId!: string;

  @OneToOne(() => TrainSet, (trainSet) => trainSet.trainSchedule)
  @JoinColumn({ name: 'train_set_id' })
  trainSet?: TrainSet;

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

  @ManyToOne(() => Route)
  @JoinColumn({ name: 'route_id' })
  route?: Route | null;

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

  @ManyToOne(() => Yard)
  @JoinColumn({ name: 'origin_station_id' })
  originStation?: Yard;

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

  @ManyToOne(() => Yard)
  @JoinColumn({ name: 'destination_station_id' })
  destinationStation?: Yard;

  @Column({ name: 'scheduled_departure_date', type: 'timestamptz' })
  scheduledDepartureDate!: Date;

  @Column({ name: 'scheduled_arrival_date', type: 'timestamptz', nullable: true })
  scheduledArrivalDate?: Date | null;

  @Column({ name: 'status', type: 'varchar', length: 20, default: 'DRAFT' })
  status!: TrainScheduleStatus;

  @Column({ name: 'train_number', type: 'varchar', length: 20, nullable: true })
  trainNumber?: string | null;

  @Column({ name: 'direction', type: 'varchar', length: 10, nullable: true })
  direction?: string | null;

  @Column({ name: 'actual_departure_at', type: 'timestamptz', nullable: true })
  actualDepartureAt?: Date | null;

  @Column({ name: 'actual_arrival_at', type: 'timestamptz', nullable: true })
  actualArrivalAt?: Date | null;

  @Column({ name: 'prepared_by_user_id', type: 'uuid', nullable: true })
  preparedByUserId?: string | null;

  @Column({ name: 'checked_by_user_id', type: 'uuid', nullable: true })
  checkedByUserId?: string | null;

  @Column({ name: 'max_wagons', type: 'int', default: 53 })
  maxWagons!: number;

  /** OPEN = accepting/holding bookings; FULL = train filled; CLOSED = manually closed. Orthogonal to `status`. */
  @Column({ name: 'booking_window_status', type: 'varchar', length: 10, default: 'OPEN' })
  bookingWindowStatus!: string;

  @OneToMany(() => TrainScheduleBooking, (scheduleBooking) => scheduleBooking.trainSchedule)
  scheduleBookings?: TrainScheduleBooking[];
}
