import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Lashing is now BULK-only and sold per trade direction (IMPORT / EXPORT),
 * optionally narrowed to one leaf commodity. Rates that no longer fit —
 * container-scoped, or carrying no direction — cannot be mapped and are
 * retired (SUPERSEDED + soft-deleted), kept readable for snapshot history.
 * Matched on trigger, not rate_type (CONSOLIDATION shares rate_type LASHING).
 */
export class LashingBulkOnlyPerDirection2890000000000 implements MigrationInterface {
  name = 'LashingBulkOnlyPerDirection2890000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      UPDATE freight.rates
      SET status = 'SUPERSEDED',
          deleted_at = now(),
          updated_at = now()
      WHERE deleted_at IS NULL
        AND "trigger" = 'LASHING'
        AND (container_type_id IS NOT NULL
             OR trade_direction IS NULL
             OR trade_direction NOT IN ('IMPORT', 'EXPORT'));
    `);
  }

  public async down(): Promise<void> {
    // Retired rates stay retired — re-enter per-direction bulk rates instead.
  }
}
