import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Bulk tonnage at assignment time. First-mile trucks and export self-haul
 * trucks carry a planned load (tonnes + optional item count) so bulk bookings
 * draw down as vehicles are assigned — not only at the weighbridge.
 */
export class AddMileTonsQuantity2820000000000 implements MigrationInterface {
  name = 'AddMileTonsQuantity2820000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE freight.first_mile_vehicle_assignments ADD COLUMN IF NOT EXISTS tons numeric(14,3);`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.first_mile_vehicle_assignments ADD COLUMN IF NOT EXISTS quantity integer;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.customer_truck_assignments ADD COLUMN IF NOT EXISTS planned_tons numeric(14,3);`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.customer_truck_assignments ADD COLUMN IF NOT EXISTS planned_quantity integer;`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE freight.customer_truck_assignments DROP COLUMN IF EXISTS planned_quantity;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.customer_truck_assignments DROP COLUMN IF EXISTS planned_tons;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.first_mile_vehicle_assignments DROP COLUMN IF EXISTS quantity;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.first_mile_vehicle_assignments DROP COLUMN IF EXISTS tons;`,
    );
  }
}
