import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Add on-hold support to bookings.
 *
 *   - hold_reason      Free-text reason captured when ops flips
 *                      status → on_hold. Nulled on any transition
 *                      out of hold.
 *   - pre_hold_status  The status the booking was in BEFORE the hold
 *                      so Resume can send it back to the right place
 *                      (upcoming ↔ on_hold ↔ upcoming; ongoing ↔
 *                      on_hold ↔ ongoing). Cleared on every
 *                      transition out of hold.
 *
 * The `status` column itself is already `varchar(20)` (no Postgres
 * enum) so the new value `'on_hold'` needs no ALTER TYPE — just the
 * TS enum extension in booking.entity.ts.
 *
 * Idempotent: `ADD COLUMN IF NOT EXISTS` so re-running is safe.
 */
export class BookingOnHoldStatus1783900000000 implements MigrationInterface {
  name = 'BookingOnHoldStatus1783900000000';

  public async up(qr: QueryRunner): Promise<void> {
    await qr.query(
      `ALTER TABLE "bookings" ADD COLUMN IF NOT EXISTS "hold_reason" varchar(500) NULL`,
    );
    await qr.query(
      `ALTER TABLE "bookings" ADD COLUMN IF NOT EXISTS "pre_hold_status" varchar(20) NULL`,
    );
  }

  public async down(qr: QueryRunner): Promise<void> {
    await qr.query(`ALTER TABLE "bookings" DROP COLUMN IF EXISTS "pre_hold_status"`);
    await qr.query(`ALTER TABLE "bookings" DROP COLUMN IF EXISTS "hold_reason"`);
  }
}
