import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * container_types.wagons_per_unit is no longer stored: the wagon fraction is
 * derived from size_ft everywhere (40ft = 1.00 wagon, 20ft = 0.50 — two per
 * wagon; see rule-engine/container-type.util.ts). The stored value duplicated
 * that rule and could silently drift from it.
 */
export class DropContainerWagonsPerUnit2290000000000 implements MigrationInterface {
  name = 'DropContainerWagonsPerUnit2290000000000';

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

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.container_types
        ADD COLUMN IF NOT EXISTS wagons_per_unit numeric(4,2);
    `);
    // Backfill from the same size rule the code now derives from.
    await queryRunner.query(`
      UPDATE freight.container_types
         SET wagons_per_unit = CASE WHEN size_ft >= 40 THEN 1.00 ELSE 0.50 END;
    `);
  }
}
