<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Task2307271756 extends AbstractMigration
{
    public function up(): void
    {
        $row = $this->fetchRow('SELECT id FROM seguro.propostas WHERE id = 851411');
        if ($row['id']) {
            $table = $this->table('seguro.propostas_parcelas');
            $singleRow = [
                           'id_usuario_criacao'    => 2068
                         , 'dt_criacao'            => 'NOW()'
                         , 'id_proposta'           => 851411
                         , 'nr_parcela'            => 1
                         , 'vl_total'              => 4592.64
                         , 'vl_segurado'           => 4592.64
                         , 'vl_subvencao_federal'  => 0
                         , 'vl_subvencao_estadual' => 0
                         , 'dt_vencimento'         => '2023-07-22'
                         , 'fl_pago'               => 'FALSE'
                         , 'id_endosso'            => 695245
            ];
            $table->insert($singleRow)
                  ->saveData();

            $table = $this->table('seguro.propostas_parcelas');
            $singleRow = [
                           'id_usuario_criacao'    => 2068
                         , 'dt_criacao'            => 'NOW()'
                         , 'id_proposta'           => 851411
                         , 'nr_parcela'            => 2
                         , 'vl_total'              => 1148.16
                         , 'vl_segurado'           => 0
                         , 'vl_subvencao_federal'  => 1148.16
                         , 'vl_subvencao_estadual' => 0
                         , 'dt_vencimento'         => '2023-07-22'
                         , 'fl_pago'               => 'FALSE'
                         , 'id_endosso'            => 695245
            ];
            $table->insert($singleRow)
                  ->saveData();
        }
    }

    public function down(): void
    {
        $row = $this->fetchRow('SELECT id FROM seguro.propostas WHERE id = 851411');
        if ($row['id']) {
            $delete = $this->getQueryBuilder();
            $delete->delete('seguro.propostas_parcelas')
               ->where([ 'id_proposta' => 851411
                       , 'nr_parcela'  => 1
                      ])
               ->execute();

            $delete = $this->getQueryBuilder();
            $delete->delete('seguro.propostas_parcelas')
               ->where([ 'id_proposta' => 851411
                       , 'nr_parcela'  => 2
                      ])
               ->execute();
        }
    }
}
