import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

export class AddVehicleCodeAndPlates1810000000002 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    const hasCode = await queryRunner.hasColumn('freight.vehicles', 'code');
    if (!hasCode) {
      await queryRunner.addColumn(
        'freight.vehicles',
        new TableColumn({ name: 'code', type: 'varchar', isNullable: true }),
      );
    }

    const hasPower = await queryRunner.hasColumn('freight.vehicles', 'power_plate_no');
    if (!hasPower) {
      await queryRunner.addColumn(
        'freight.vehicles',
        new TableColumn({ name: 'power_plate_no', type: 'varchar', isNullable: true }),
      );
    }

    const hasTrailer = await queryRunner.hasColumn('freight.vehicles', 'trailer_plate_no');
    if (!hasTrailer) {
      await queryRunner.addColumn(
        'freight.vehicles',
        new TableColumn({ name: 'trailer_plate_no', type: 'varchar', isNullable: true }),
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropColumn('freight.vehicles', 'trailer_plate_no');
    await queryRunner.dropColumn('freight.vehicles', 'power_plate_no');
    await queryRunner.dropColumn('freight.vehicles', 'code');
  }
}
