import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Collect Payment — receive-currency conversion fields.
 *
 * Booking/invoice stays in bill currency (`currency_id`). The client
 * may pay in another currency; accounts enter the received amount in
 * that currency plus an exchange rate. `received_amount` remains the
 * bill-currency figure used for allocation.
 *
 * Applied to office collections (`payment_collections`) and
 * Collect-for-MD pending batches (`booking_cash_collection_batches`).
 */
export class CollectReceiveCurrency1786000000000 implements MigrationInterface {
  name = 'CollectReceiveCurrency1786000000000';

  public async up(qr: QueryRunner): Promise<void> {
    await qr.query(
      `ALTER TABLE "payment_collections" ADD COLUMN "receive_currency_id" uuid NULL`,
    );
    await qr.query(
      `ALTER TABLE "payment_collections" ADD COLUMN "exchange_rate" numeric(12,6) NULL`,
    );
    await qr.query(
      `ALTER TABLE "payment_collections" ADD COLUMN "received_amount_receive_currency" numeric(12,2) NULL`,
    );
    await qr.query(`
      ALTER TABLE "payment_collections"
        ADD CONSTRAINT "fk_pc_receive_currency"
          FOREIGN KEY ("receive_currency_id") REFERENCES "currencies"("id"),
        ADD CONSTRAINT "chk_pc_exchange_rate_non_neg"
          CHECK ("exchange_rate" IS NULL OR "exchange_rate" >= 0),
        ADD CONSTRAINT "chk_pc_received_amount_receive_currency_non_neg"
          CHECK (
            "received_amount_receive_currency" IS NULL
            OR "received_amount_receive_currency" >= 0
          )
    `);

    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" ADD COLUMN "receive_currency_id" uuid NULL`,
    );
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" ADD COLUMN "exchange_rate" numeric(12,6) NULL`,
    );
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" ADD COLUMN "received_amount_receive_currency" numeric(12,2) NULL`,
    );
    await qr.query(`
      ALTER TABLE "booking_cash_collection_batches"
        ADD CONSTRAINT "fk_bcc_batch_receive_currency"
          FOREIGN KEY ("receive_currency_id") REFERENCES "currencies"("id"),
        ADD CONSTRAINT "chk_bcc_batch_exchange_rate_non_neg"
          CHECK ("exchange_rate" IS NULL OR "exchange_rate" >= 0),
        ADD CONSTRAINT "chk_bcc_batch_received_amount_receive_currency_non_neg"
          CHECK (
            "received_amount_receive_currency" IS NULL
            OR "received_amount_receive_currency" >= 0
          )
    `);
  }

  public async down(qr: QueryRunner): Promise<void> {
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" DROP CONSTRAINT IF EXISTS "chk_bcc_batch_received_amount_receive_currency_non_neg"`,
    );
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" DROP CONSTRAINT IF EXISTS "chk_bcc_batch_exchange_rate_non_neg"`,
    );
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" DROP CONSTRAINT IF EXISTS "fk_bcc_batch_receive_currency"`,
    );
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" DROP COLUMN IF EXISTS "received_amount_receive_currency"`,
    );
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" DROP COLUMN IF EXISTS "exchange_rate"`,
    );
    await qr.query(
      `ALTER TABLE "booking_cash_collection_batches" DROP COLUMN IF EXISTS "receive_currency_id"`,
    );

    await qr.query(
      `ALTER TABLE "payment_collections" DROP CONSTRAINT IF EXISTS "chk_pc_received_amount_receive_currency_non_neg"`,
    );
    await qr.query(
      `ALTER TABLE "payment_collections" DROP CONSTRAINT IF EXISTS "chk_pc_exchange_rate_non_neg"`,
    );
    await qr.query(
      `ALTER TABLE "payment_collections" DROP CONSTRAINT IF EXISTS "fk_pc_receive_currency"`,
    );
    await qr.query(
      `ALTER TABLE "payment_collections" DROP COLUMN IF EXISTS "received_amount_receive_currency"`,
    );
    await qr.query(
      `ALTER TABLE "payment_collections" DROP COLUMN IF EXISTS "exchange_rate"`,
    );
    await qr.query(
      `ALTER TABLE "payment_collections" DROP COLUMN IF EXISTS "receive_currency_id"`,
    );
  }
}
