import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Destination-currency accounting (client 2026-11-05): customer bills are
 * kept in the destination's currency whatever currency the customer is
 * billed in. Stores the pre-rounding destination total on quotes and
 * bookings, and on each bill the selling amount, its currency and the
 * rate used. All nullable — no backfill (existing bills are converted
 * separately after review).
 */
export class DestinationCurrencyAccounting1789000000000 implements MigrationInterface {
  name = 'DestinationCurrencyAccounting1789000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "bookings" ADD COLUMN IF NOT EXISTS "selling_total_dest" numeric(12,2)`,
    );
    await queryRunner.query(
      `ALTER TABLE "query_quotes" ADD COLUMN IF NOT EXISTS "selling_total_dest" numeric(12,2)`,
    );
    await queryRunner.query(
      `ALTER TABLE "payment_settlements" ADD COLUMN IF NOT EXISTS "selling_amount" numeric(12,2)`,
    );
    await queryRunner.query(
      `ALTER TABLE "payment_settlements" ADD COLUMN IF NOT EXISTS "selling_currency_id" uuid`,
    );
    await queryRunner.query(
      `ALTER TABLE "payment_settlements" ADD COLUMN IF NOT EXISTS "exchange_rate" numeric(12,6)`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "payment_settlements" DROP COLUMN IF EXISTS "exchange_rate"`,
    );
    await queryRunner.query(
      `ALTER TABLE "payment_settlements" DROP COLUMN IF EXISTS "selling_currency_id"`,
    );
    await queryRunner.query(
      `ALTER TABLE "payment_settlements" DROP COLUMN IF EXISTS "selling_amount"`,
    );
    await queryRunner.query(
      `ALTER TABLE "query_quotes" DROP COLUMN IF EXISTS "selling_total_dest"`,
    );
    await queryRunner.query(`ALTER TABLE "bookings" DROP COLUMN IF EXISTS "selling_total_dest"`);
  }
}
