import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Partial-batch splits no longer promote a ONE_TIME contract to GENERAL.
 * Instead the reduced booking is flagged is_split, and the booking gate lets
 * the customer book exactly the remainder under the still-ONE_TIME contract.
 */
export class AddBookingIsSplit2110000000000 implements MigrationInterface {
  name = 'AddBookingIsSplit2110000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.bookings
        ADD COLUMN IF NOT EXISTS is_split BOOLEAN NOT NULL DEFAULT FALSE
    `);
    // Quantities the booking carried before the split — the remainder ledger
    // for ONE_TIME contracts, which have no quantity cap to derive it from.
    await queryRunner.query(`
      ALTER TABLE freight.bookings
        ADD COLUMN IF NOT EXISTS pre_split_quantities JSONB NULL
    `);
  }

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