<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Task2109231746 extends AbstractMigration
{
    public function up(): void
    {
        $table = $this->table('manutencao.insere_proponente');
        $rows = [
                    ['nome' => 'ALBERT JAN SNOEIJER'                          , 'tipo' => 'PF', 'documento' => '04152335987'   , 'sexo' => 'M'],
                    ['nome' => 'ALEXANDRA BIERSTEKER MACHADO'                 , 'tipo' => 'PF', 'documento' => '04438711960'   , 'sexo' => 'F'],
                    ['nome' => 'ANDRE SCHMIDT DE SOUZA'                       , 'tipo' => 'PF', 'documento' => '07368575914'   , 'sexo' => 'M'],
                    ['nome' => 'CORNELIO JOAO HARMS'                          , 'tipo' => 'PF', 'documento' => '41070828904'   , 'sexo' => 'M'],
                    ['nome' => 'CLAUDIO KOCH'                                 , 'tipo' => 'PF', 'documento' => '03329520965'   , 'sexo' => 'M'],
                    ['nome' => 'KAROLINE BARTH'                               , 'tipo' => 'PF', 'documento' => '08287286905'   , 'sexo' => 'F'],
                    ['nome' => 'MARIA LUIZA BRANDES BARTH'                    , 'tipo' => 'PF', 'documento' => '85461377904'   , 'sexo' => 'F'],
                    ['nome' => 'MATEUS BRANDES'                               , 'tipo' => 'PF', 'documento' => '68814305900'   , 'sexo' => 'M'],
                    ['nome' => 'RAFAEL JUSTUS BUHRER'                         , 'tipo' => 'PF', 'documento' => '00418178933'   , 'sexo' => 'M'],
                    ['nome' => 'MARCELO JORGE FADEL'                          , 'tipo' => 'PF', 'documento' => '70494940972'   , 'sexo' => 'M'],
                    ['nome' => 'ANDRE LEMES GALVÃO'                           , 'tipo' => 'PF', 'documento' => '02838831976'   , 'sexo' => 'M'],
                    ['nome' => 'JOSE CARLOS SGUARIO JUNIOR'                   , 'tipo' => 'PF', 'documento' => '70948437987'   , 'sexo' => 'M'],
                    ['nome' => 'GUSTAVO PILATTI CORREIA'                      , 'tipo' => 'PF', 'documento' => '05404780916'   , 'sexo' => 'M'],
                    ['nome' => 'JORGE L. VALLE NICOLAU'                       , 'tipo' => 'PF', 'documento' => '51917580991'   , 'sexo' => 'M'],
                    ['nome' => 'JOAO CARLOS DE MATTOS JR'                     , 'tipo' => 'PF', 'documento' => '03550983930'   , 'sexo' => 'M'],
                    ['nome' => 'ROBERTO ABREU DE AGUIAR'                      , 'tipo' => 'PF', 'documento' => '66667321972'   , 'sexo' => 'M'],
                    ['nome' => 'UNIVERSIDADE ESTADUAL DE PONTA GROSSA'        , 'tipo' => 'PJ', 'documento' => '80257355000108', 'sexo' => 'M'],
                    ['nome' => 'RAFAEL HUBERT'                                , 'tipo' => 'PF', 'documento' => '04045752935'   , 'sexo' => 'M'],
                    ['nome' => 'AGROMAXSUL EMPREENDIMENTOS LTDA'              , 'tipo' => 'PJ', 'documento' => '36857046000179', 'sexo' => 'M'],
                    ['nome' => 'MARCO ANTONIO RODERJAN CARNEIRO'              , 'tipo' => 'PF', 'documento' => '76670732953'   , 'sexo' => 'M'],
                    ['nome' => 'GLAUBER DE MELLO GIORDANI'                    , 'tipo' => 'PF', 'documento' => '07841417034'   , 'sexo' => 'M'],
                    ['nome' => 'RAFAEL GOMES DA SILVA GORDO'                  , 'tipo' => 'PF', 'documento' => '07272136952'   , 'sexo' => 'M'],
                    ['nome' => 'RAFAEL NADAL NASCIMENTO'                      , 'tipo' => 'PF', 'documento' => '05451441910'   , 'sexo' => 'M'],
                    ['nome' => 'ANTONIO CARLOS BERNARDINO'                    , 'tipo' => 'PF', 'documento' => '52273849915'   , 'sexo' => 'M'],
                    ['nome' => 'JAIR ROBERTO ZARPELLON'                       , 'tipo' => 'PF', 'documento' => '21054657904'   , 'sexo' => 'M'],
                    ['nome' => 'MARCIO PETRONIO DA COSTA'                     , 'tipo' => 'PF', 'documento' => '03177210977'   , 'sexo' => 'M'],
                    ['nome' => 'RAFAEL SEGISMUNDO S.'                         , 'tipo' => 'PF', 'documento' => '05374451960'   , 'sexo' => 'M'],
                    ['nome' => 'JOSE CLEO ULSENHEIMER'                        , 'tipo' => 'PF', 'documento' => '03865439304'   , 'sexo' => 'M'],
                    ['nome' => 'CORNELIO HAROLDO DYJKSTRA'                    , 'tipo' => 'PF', 'documento' => '56885270997'   , 'sexo' => 'M'],
                    ['nome' => 'SIMONE SPINARDI'                              , 'tipo' => 'PF', 'documento' => '00700793909'   , 'sexo' => 'F'],
                    ['nome' => 'ARIEL RICKLI'                                 , 'tipo' => 'PF', 'documento' => '02678695921'   , 'sexo' => 'M'],
                    ['nome' => 'AGROMAXSUL EMPREENDIMENTOS AGROPECUARIOS LTDA', 'tipo' => 'PJ', 'documento' => '36857046000179', 'sexo' => 'M'],
                    ['nome' => 'JOAO CARLOS DE MATTOS JUNIOR'                 , 'tipo' => 'PF', 'documento' => '03550983930'   , 'sexo' => 'M'],
                    ['nome' => 'RAFAEL JUSTUS BUHRER'                         , 'tipo' => 'PF', 'documento' => '00418178933'   , 'sexo' => 'M']
        ];
        $table->insert($rows)
              ->save();
        
        $this->execute("CREATE OR REPLACE FUNCTION manutencao.insere_proponente() RETURNS VOID AS $$
                        DECLARE
                            stSQL       VARCHAR;
                            reRecord    RECORD;
                            inCount     INTEGER;
                        BEGIN
                            inCount := 0;
                            stSQL := '
                                         SELECT LTRIM(tipo, ''P'') AS tipo
                                              , sexo
                                              , nome
                                              , documento
                                              , CASE
                                                    WHEN tipo = ''PF'' THEN
                                                        REGEXP_REPLACE(LPAD(documento,11,''0''), ''([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{2})'', ''\1.\2.\3-\4'')
                                                    ELSE
                                                        REGEXP_REPLACE(LPAD(documento,14,''0''), ''([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{2})'', ''\1.\2.\3/\4-\5'')
                                                END AS cpf_cnpj
                                       --     , documento AS cpf_cnpj
                                           FROM manutencao.insere_proponente
                                       GROUP BY 1, 2, 3, 4, 5
                                              ;
                                     ';
                            FOR reRecord IN EXECUTE stSQL LOOP
                                RAISE NOTICE '%    DOC: % - %  %', reRecord.tipo, reRecord.cpf_cnpj, reRecord.sexo, reRecord.nome;
                                PERFORM 1
                                   FROM seguro.proponentes
                                  WHERE cpf_cnpj = reRecord.cpf_cnpj
                                      ;
                                IF NOT FOUND THEN
                                    inCount := inCount + 1;
                                    INSERT
                                      INTO seguro.proponentes
                                         ( id_usuario_criacao
                                         , dt_criacao
                                         , ds_sexo
                                         , cpf_cnpj
                                         , ds_nome_proponente
                                         )
                                    VALUES
                                         ( 2530
                                         , NOW()
                                         , reRecord.sexo
                                         , reRecord.cpf_cnpj
                                         , reRecord.nome
                                         );
                                    UPDATE manutencao.insere_proponente
                                       SET count = inCount
                                     WHERE documento = reRecord.documento
                                         ;
                                    RAISE NOTICE 'COUNT: %', inCount;
                                ELSE
                                    RAISE NOTICE 'NAO INSERE ----------------------- % - %', reRecord.cpf_cnpj, reRecord.nome;
                                    UPDATE manutencao.insere_proponente
                                       SET inserido = FALSE
                                     WHERE documento = reRecord.documento
                                         ;
                                END IF;
                            END LOOP;
                        END;
                        $$ LANGUAGE 'plpgsql';
                      ");
        
        $this->execute("SELECT manutencao.insere_proponente()");
    }

    public function down(): void
    {

    }
}
