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

/**
 * Batch 8 — train-arrival unload landing state on warehouse_inventory:
 *  - unloaded_at: when the goods were unloaded off the arrived train (before storage/inspection)
 *
 * The `status` column is a free varchar, so the new 'UNLOADED' value needs no schema change.
 * Idempotent: the shared dev DB may already carry this column (added by another checkout).
 */
export class AddInventoryUnloadedAt1791000000003 implements MigrationInterface {
  private readonly table = 'freight.warehouse_inventory';

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

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