import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Customs clearance fees are now sold per cargo kind: container fees name a
 * container type (billed PER_CONTAINER / PER_WAGON), bulk fees carry no type
 * (billed PER_TON / PER_WAGON). The old one-FLAT-fee-per-route shape cannot be
 * mapped to a kind — retired (SUPERSEDED + soft-deleted) exactly like the
 * base-freight and return-surcharge reshapes, kept readable for snapshot
 * history. Per-kind replacements must be re-entered; a customs contract or
 * booking without a matching fee hard-blocks. Contracts that already froze a
 * FLAT snapshot keep billing it (legacy honoured at booking pricing).
 */
export class CustomsClearancePerKind2870000000000 implements MigrationInterface {
  name = 'CustomsClearancePerKind2870000000000';

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

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