import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Transit-assignee handshake before the customs declaration.
 *
 * GL Ethiopia must ask GL Djibouti who will handle the shipment in transit, and
 * Djibouti answers with a name, before the declaration can be filed. The whole
 * exchange lives on the clearance cycle so it repeats naturally with each cycle
 * of a GENERAL contract.
 */
export class AddTransitAssigneeHandshake2950000000000
  implements MigrationInterface
{
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.contract_clearance_cycles
        ADD COLUMN IF NOT EXISTS transit_assignee_requested_at timestamptz NULL,
        ADD COLUMN IF NOT EXISTS transit_assignee_requested_by_user_id uuid NULL,
        ADD COLUMN IF NOT EXISTS transit_assignee_request_note text NULL,
        ADD COLUMN IF NOT EXISTS transit_assignee_name text NULL,
        ADD COLUMN IF NOT EXISTS transit_assignee_assigned_at timestamptz NULL,
        ADD COLUMN IF NOT EXISTS transit_assignee_assigned_by_user_id uuid NULL
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.contract_clearance_cycles
        DROP COLUMN IF EXISTS transit_assignee_requested_at,
        DROP COLUMN IF EXISTS transit_assignee_requested_by_user_id,
        DROP COLUMN IF EXISTS transit_assignee_request_note,
        DROP COLUMN IF EXISTS transit_assignee_name,
        DROP COLUMN IF EXISTS transit_assignee_assigned_at,
        DROP COLUMN IF EXISTS transit_assignee_assigned_by_user_id
    `);
  }
}
