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

export class AddPostPaymentCompletedColumn1810000000004 implements MigrationInterface {
  name = 'AddPostPaymentCompletedColumn1810000000004';

  public async up(queryRunner: QueryRunner): Promise<void> {
    const firstMileTable = await queryRunner.hasTable('freight.first_mile_deliveries');
    if (firstMileTable) {
      const hasColumn = await queryRunner.hasColumn('freight.first_mile_deliveries', 'is_post_payment_completed');
      if (!hasColumn) {
        await queryRunner.addColumn(
          'freight.first_mile_deliveries',
          new TableColumn({
            name: 'is_post_payment_completed',
            type: 'boolean',
            default: false,
            isNullable: false,
          })
        );
      }
    }

    const lastMileTable = await queryRunner.hasTable('freight.last_mile_deliveries');
    if (lastMileTable) {
      const hasColumn = await queryRunner.hasColumn('freight.last_mile_deliveries', 'is_post_payment_completed');
      if (!hasColumn) {
        await queryRunner.addColumn(
          'freight.last_mile_deliveries',
          new TableColumn({
            name: 'is_post_payment_completed',
            type: 'boolean',
            default: false,
            isNullable: false,
          })
        );
      }
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const lastMileTable = await queryRunner.hasTable('freight.last_mile_deliveries');
    if (lastMileTable) {
      await queryRunner.dropColumn('freight.last_mile_deliveries', 'is_post_payment_completed');
    }

    const firstMileTable = await queryRunner.hasTable('freight.first_mile_deliveries');
    if (firstMileTable) {
      await queryRunner.dropColumn('freight.first_mile_deliveries', 'is_post_payment_completed');
    }
  }
}
