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

@Entity({ schema: 'freight', name: 'train_scheduling_global_rules' })
export class TrainSchedulingGlobalRules extends BaseEntity {
  @Column({
    name: 'max_train_length_meters',
    type: 'numeric',
    precision: 10,
    scale: 2,
    default: 760,
  })
  maxTrainLengthMeters!: number;

  @Column({
    name: 'max_train_weight_tons',
    type: 'numeric',
    precision: 10,
    scale: 3,
    default: 3500,
  })
  maxTrainWeightTons!: number;

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

  @Column({
    name: 'max_20ft_container_weight_tons',
    type: 'numeric',
    precision: 8,
    scale: 3,
    default: 30,
  })
  max20ftContainerWeightTons!: number;

  @Column({
    name: 'max_20ft_pair_weight_diff_tons',
    type: 'numeric',
    precision: 8,
    scale: 3,
    default: 10,
  })
  max20ftPairWeightDiffTons!: number;

  /** Days before departure the single import booking-window day falls on. */
  @Column({ name: 'import_window_lead_days', type: 'int', default: 3 })
  importWindowLeadDays!: number;

  /** Hours before departure an export booking becomes acceptable (FCFS, no window cycle). */
  @Column({ name: 'export_booking_lead_hours', type: 'int', default: 24 })
  exportBookingLeadHours!: number;

  /** Local (Africa/Addis_Ababa) hour at which the import window opens on its window day. */
  @Column({ name: 'window_open_hour', type: 'int', default: 8 })
  windowOpenHour!: number;

  /**
   * Local (Africa/Addis_Ababa) hour the booking desk shuts each day. A not-yet-full
   * train whose next cycle would reopen at/after this hour pauses until the next
   * morning's windowOpenHour. Set equal to windowOpenHour for a 24-hour desk.
   */
  @Column({ name: 'window_close_hour', type: 'int', default: 17 })
  windowCloseHour!: number;

  // Stored in hours; 4 decimals so sub-minute UI durations (4 min = 0.0667h)
  // are exact. See WidenWindowDurationHoursPrecision migration.
  @Column({
    name: 'window_duration_hours',
    type: 'numeric',
    precision: 6,
    scale: 4,
    default: 3,
  })
  windowDurationHours!: number;

  /** Max time staff have to accept booking documents after the window closes. */
  @Column({ name: 'doc_review_minutes', type: 'int', default: 30 })
  docReviewMinutes!: number;

  @Column({ name: 'payment_window_minutes', type: 'int', default: 60 })
  paymentWindowMinutes!: number;

  /**
   * Minutes before departure the IMPORT/DOMESTIC booking window shuts. When set,
   * the window's close (first cycle and every reopen) is capped at
   * `departure − this`, instead of the default open+duration/departure cap.
   * NULL or 0 = no offset (previous behaviour).
   */
  @Column({ name: 'import_close_offset_minutes', type: 'int', nullable: true })
  importCloseOffsetMinutes?: number | null;

  /**
   * Minutes before departure the EXPORT FCFS booking window shuts. When set, the
   * export window closes at `departure − this` instead of at departure. NULL or
   * 0 = no offset (export closes at departure, previous behaviour).
   */
  @Column({ name: 'export_close_offset_minutes', type: 'int', nullable: true })
  exportCloseOffsetMinutes?: number | null;
}
