import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Djibouti GL must record WHEN the vessel arrived and WHEN the Delivery Order
 * was collected, not just attach the DO file. Both are mandatory on DO upload
 * (enforced in the clearance services), so the columns are new and nullable —
 * DOs uploaded before this change have no dates to backfill.
 *
 * `vessel_departure_date` is the EXPORT Release-Order date and stays as-is; the
 * import arrival date gets its own column rather than overloading it.
 */
export class AddDoCollectionDates2970000000000 implements MigrationInterface {
  name = 'AddDoCollectionDates2970000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    for (const table of [
      'freight.contract_clearance_cycles',
      'freight.bookings',
    ]) {
      await queryRunner.query(
        `ALTER TABLE ${table} ADD COLUMN IF NOT EXISTS vessel_arrival_date date;`,
      );
      await queryRunner.query(
        `ALTER TABLE ${table} ADD COLUMN IF NOT EXISTS do_collected_date date;`,
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    for (const table of [
      'freight.contract_clearance_cycles',
      'freight.bookings',
    ]) {
      await queryRunner.query(
        `ALTER TABLE ${table} DROP COLUMN IF EXISTS do_collected_date;`,
      );
      await queryRunner.query(
        `ALTER TABLE ${table} DROP COLUMN IF EXISTS vessel_arrival_date;`,
      );
    }
  }
}
