import { MigrationInterface, QueryRunner } from "typeorm";

/**
 * Add the `EXPIRED` invoice status. An invoice expires when its source's pay
 * window closes before settlement (e.g. a booking whose `paymentDeadline`
 * lapses) — driven event-style from the domain via `BillingService.expirePayable`,
 * which emits `${source}.invoice.expired`. Terminal and not settle-able (kept out
 * of `OPEN_STATUSES`), so it is distinct from `CANCELLED` (manual void) and
 * `OVERDUE` (still payable).
 *
 * Matches Freight.InvoiceStatus in packages/types. ADD VALUE only — additive and
 * not referenced in this same transaction, so it is PG 12+ safe.
 */
export class AddExpiredInvoiceStatus1830000000000 implements MigrationInterface {
  name = "AddExpiredInvoiceStatus1830000000000";

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TYPE freight.invoices_status_enum ADD VALUE IF NOT EXISTS 'EXPIRED' AFTER 'REFUNDED';`,
    );
  }

  public async down(): Promise<void> {
    // Postgres cannot drop individual enum values; EXPIRED is left on
    // freight.invoices_status_enum (harmless, unused after down).
  }
}
