import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Align weight_limit_rules.trade_direction with app code: IMPORT, EXPORT, BOTH (not ANY).
 */
export class NormalizeWeightLimitTradeDirectionBoth1749000000000
  implements MigrationInterface
{
  name = 'NormalizeWeightLimitTradeDirectionBoth1749000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      DO $$ BEGIN
        UPDATE freight.weight_limit_rules
        SET trade_direction = 'BOTH'
        WHERE trade_direction::text = 'ANY';

        UPDATE freight.weight_limit_rules
        SET trade_direction = 'IMPORT'
        WHERE trade_direction IS NULL;
      EXCEPTION WHEN undefined_table OR undefined_column THEN NULL;
      END $$;
    `);
  }

  public async down(_queryRunner: QueryRunner): Promise<void> {
    // No-op: ANY is not a valid enum value in PostgreSQL.
  }
}
