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

/**
 * Collect Payment debit/credit bank accounts.
 *
 * Non-cash modes store two distinct bank_accounts FKs on the collection
 * (and on MD pending batches so they survive until confirm). CASH keeps
 * both columns null. Distinct-IDs CHECK allows both-null (cash / legacy).
 *
 * Idempotent: local `synchronize: true` may already have added the
 * columns before this migration ran.
 */
export class CollectBankAccounts1786200000000 implements MigrationInterface {
  name = 'CollectBankAccounts1786200000000';

  public async up(qr: QueryRunner): Promise<void> {
    await qr.query(`
      ALTER TABLE payment_collections
        ADD COLUMN IF NOT EXISTS debit_bank_account_id uuid,
        ADD COLUMN IF NOT EXISTS credit_bank_account_id uuid;
    `);
    await qr.query(`
      DO $$ BEGIN
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'fk_pc_debit_bank_account'
        ) THEN
          ALTER TABLE payment_collections
            ADD CONSTRAINT fk_pc_debit_bank_account
            FOREIGN KEY (debit_bank_account_id) REFERENCES bank_accounts(id);
        END IF;
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'fk_pc_credit_bank_account'
        ) THEN
          ALTER TABLE payment_collections
            ADD CONSTRAINT fk_pc_credit_bank_account
            FOREIGN KEY (credit_bank_account_id) REFERENCES bank_accounts(id);
        END IF;
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'chk_pc_debit_credit_distinct'
        ) THEN
          ALTER TABLE payment_collections
            ADD CONSTRAINT chk_pc_debit_credit_distinct
            CHECK (
              debit_bank_account_id IS NULL
              OR credit_bank_account_id IS NULL
              OR debit_bank_account_id <> credit_bank_account_id
            );
        END IF;
      END $$;
    `);

    await qr.query(`
      ALTER TABLE booking_cash_collection_batches
        ADD COLUMN IF NOT EXISTS debit_bank_account_id uuid,
        ADD COLUMN IF NOT EXISTS credit_bank_account_id uuid;
    `);
    await qr.query(`
      DO $$ BEGIN
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'fk_bcc_batch_debit_bank_account'
        ) THEN
          ALTER TABLE booking_cash_collection_batches
            ADD CONSTRAINT fk_bcc_batch_debit_bank_account
            FOREIGN KEY (debit_bank_account_id) REFERENCES bank_accounts(id);
        END IF;
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'fk_bcc_batch_credit_bank_account'
        ) THEN
          ALTER TABLE booking_cash_collection_batches
            ADD CONSTRAINT fk_bcc_batch_credit_bank_account
            FOREIGN KEY (credit_bank_account_id) REFERENCES bank_accounts(id);
        END IF;
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'chk_bcc_batch_debit_credit_distinct'
        ) THEN
          ALTER TABLE booking_cash_collection_batches
            ADD CONSTRAINT chk_bcc_batch_debit_credit_distinct
            CHECK (
              debit_bank_account_id IS NULL
              OR credit_bank_account_id IS NULL
              OR debit_bank_account_id <> credit_bank_account_id
            );
        END IF;
      END $$;
    `);
  }

  public async down(qr: QueryRunner): Promise<void> {
    await qr.query(`
      ALTER TABLE booking_cash_collection_batches
        DROP CONSTRAINT IF EXISTS chk_bcc_batch_debit_credit_distinct,
        DROP CONSTRAINT IF EXISTS fk_bcc_batch_credit_bank_account,
        DROP CONSTRAINT IF EXISTS fk_bcc_batch_debit_bank_account,
        DROP COLUMN IF EXISTS credit_bank_account_id,
        DROP COLUMN IF EXISTS debit_bank_account_id;
    `);
    await qr.query(`
      ALTER TABLE payment_collections
        DROP CONSTRAINT IF EXISTS chk_pc_debit_credit_distinct,
        DROP CONSTRAINT IF EXISTS fk_pc_credit_bank_account,
        DROP CONSTRAINT IF EXISTS fk_pc_debit_bank_account,
        DROP COLUMN IF EXISTS credit_bank_account_id,
        DROP COLUMN IF EXISTS debit_bank_account_id;
    `);
  }
}
