import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Double-handling fee support. warehouse_fee_rules.basis: how a
 * DOUBLE_HANDLING_FEE rule is charged — PER_CONTAINER | PER_TON | PER_ITEM
 * (null for the day-based fee types). The PER_TON / PER_ITEM quantity comes from
 * the booking's cargo total (cargo_total_weight_vgm, expressed in the cargo's
 * unit of measure), so no new booking column is needed.
 */
export class AddDoubleHandlingBasisAndMachinery1990000000000 implements MigrationInterface {
  name = 'AddDoubleHandlingBasisAndMachinery1990000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE freight.warehouse_fee_rules ADD COLUMN IF NOT EXISTS basis varchar(20)`,
    );
    // machinery_units is not used (PER_ITEM reads cargo_total_weight_vgm); drop it
    // if a prior version of this migration added it.
    await queryRunner.query(`ALTER TABLE freight.bookings DROP COLUMN IF EXISTS machinery_units`);
  }

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