<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Task2303171640V2 extends AbstractMigration
{
    public function up(): void
    {
        $this->execute(<<<SQL
CREATE TEMP TABLE beneficiarios_banco AS (
     SELECT sib.cpf_cnpj, sib.id_proposta
       FROM seguro.informacoes_bancarias sib
      WHERE EXISTS(SELECT 1
                     FROM sistema.agencias sa
                    WHERE sa.ds_cnpj = sib.cpf_cnpj)
        AND (nu_agencia <> '' OR nu_digito_conta_corrente <> '' OR nu_agencia <> '' OR nu_digito_agencia <> '' OR id_forma_pagamento <> 2));

UPDATE seguro.informacoes_bancarias sib
   SET nu_conta_corrente = '', nu_digito_conta_corrente = '', nu_agencia = '', nu_digito_agencia = '', id_forma_pagamento = 2
  FROM beneficiarios_banco bb
 WHERE bb.cpf_cnpj = sib.cpf_cnpj
   AND bb.id_proposta = sib.id_proposta;

UPDATE seguro.propostas_beneficiarios spb
SET ds_agencia = '', ds_conta = ''
FROM beneficiarios_banco bb
WHERE bb.cpf_cnpj = spb.nr_cpf_cnpj
  AND bb.id_proposta = spb.id_proposta;
SQL
);
    }

    public function down(): void
    {
    }
}
