import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Lashing is now sold per cargo kind, like the customs clearance fee:
 * container rates name a container type (PER_CONTAINER / PER_WAGON), bulk
 * rates carry no type (PER_TON / PER_WAGON). The old flat-per-booking shape
 * cannot be mapped to a kind — retired (SUPERSEDED + soft-deleted), kept
 * readable for snapshot history. Per-kind replacements must be re-entered;
 * an unconfigured lashing rate simply bills nothing (lenient, like
 * hazard/reefer). Matched on trigger, not rate_type — CONSOLIDATION rates
 * share the LASHING rate_type and must survive.
 */
export class LashingPerKind2880000000000 implements MigrationInterface {
  name = 'LashingPerKind2880000000000';

  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 rate_unit = 'FLAT';
    `);
  }

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