import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Per-container handling opt-in: each physical container can now be marked
 * hazardous / reefer / with-return individually, next to its VGM. The hazardous
 * and reefer flags already existed on the unit row; only the return leg was
 * missing, so a booking of 20 containers with 10 returning empty can bill the
 * WITH_RETURN surcharge on 10 instead of all 20.
 *
 * Backfill: existing rows keep false. The line-level counts
 * (booking_container.return_quantity etc.) stay authoritative for bookings made
 * before this change — the rule engine falls back to them when no unit is flagged.
 */
export class AddContainerUnitReturnFlag2370000000000 implements MigrationInterface {
  name = 'AddContainerUnitReturnFlag2370000000000';

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

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