import { type MigrationInterface, type QueryRunner } from 'typeorm';

/**
 * Adds 'below_floor_price' to the ChangeRequestKind enum so save paths
 * can throw "submit a CR" instead of silently allowing or hard-blocking.
 *
 * ⚠ `ALTER TYPE ... ADD VALUE` cannot run inside a transaction block
 * on most Postgres setups (every version pre-12, and 12+ also rejects
 * it when committed and used in the same tx). TypeORM wraps migrations
 * in a transaction by default — so we explicitly opt out via the
 * `transaction = false` instance flag below. The ALTER becomes
 * autocommit, the migrations row is then inserted in its own tx, and
 * the runner moves on.
 *
 * Per Postgres rules, enum values can only be added — never removed —
 * so the down() is a no-op. If you need to roll back, drop the enum
 * and recreate it with a separate migration.
 */
export class AddBelowFloorPriceCRKind1777300000000 implements MigrationInterface {
  /** Disable TypeORM's default per-migration transaction wrapping. */
  transaction = false as const;

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TYPE "booking_change_requests_kind_enum" ADD VALUE IF NOT EXISTS 'below_floor_price'`,
    );
  }

  public async down(): Promise<void> {
    // no-op — Postgres can't drop a single enum value
  }
}
