import { MigrationInterface, QueryRunner } from "typeorm";

/**
 * Add an opt-in "reverse wagon order" flag to a train schedule.
 *
 * When true, the built wagon plan is flipped at build time so the physically-last
 * wagon sits at position 1. Only the order (sequence_no) changes — composition and
 * booking allocations travel with their slot. The flag is frozen on the schedule
 * at creation and re-applied every time the wagon plan is rebuilt, so the stored
 * train order and the schedule order always match.
 *
 * Defaults to false; existing schedules keep their as-built order.
 */
export class AddReverseWagonOrder2340000000000 implements MigrationInterface {
  name = "AddReverseWagonOrder2340000000000";

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.train_schedules
        ADD COLUMN IF NOT EXISTS reverse_wagon_order boolean NOT NULL DEFAULT false;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.train_schedules
        DROP COLUMN IF EXISTS reverse_wagon_order;
    `);
  }
}
