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

/**
 * Import pickup branch on warehouse_inventory:
 *  - release_order_reference: DO / release order number sent to the customer
 *  - delivered_at: when the goods were handed over (proof of delivery)
 *
 * Idempotent: the shared dev DB may already carry some of these columns
 * (added by another checkout), so only add what is missing.
 */
export class AddImportPickupDeliveryColumns1791000000002 implements MigrationInterface {
  private readonly table = 'freight.warehouse_inventory';

  public async up(queryRunner: QueryRunner): Promise<void> {
    if (!(await queryRunner.hasColumn(this.table, 'release_order_reference'))) {
      await queryRunner.addColumn(
        this.table,
        new TableColumn({ name: 'release_order_reference', type: 'varchar', length: '100', isNullable: true }),
      );
    }
    if (!(await queryRunner.hasColumn(this.table, 'delivered_at'))) {
      await queryRunner.addColumn(
        this.table,
        new TableColumn({ name: 'delivered_at', type: 'timestamptz', isNullable: true }),
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    if (await queryRunner.hasColumn(this.table, 'release_order_reference')) {
      await queryRunner.dropColumn(this.table, 'release_order_reference');
    }
    if (await queryRunner.hasColumn(this.table, 'delivered_at')) {
      await queryRunner.dropColumn(this.table, 'delivered_at');
    }
  }
}
