<?php


namespace App\Models;

use App\Entities\AtendimentoFormulario\AtendimentoFormulario;
use App\Entities\ProcedimentoFormulario\ProcedimentoFormulario;
use App\Utils\Collections\GeneralCollection;
use App\Utils\DateUtils;
use CodeIgniter\Model;

class AtendimentoFormularioModel extends Model
{

    protected $table = 'formulario_atendimento';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $allowedFields = [
        'id_formulario',
    ];
    protected $useSoftDeletes = false;
    protected $skipValidation = true;
    protected $useTimestamps = false;
    protected $createdField = 'criado_em';
    protected $updatedField = 'atualizado_em';

    public function registerAtendimentoFormularioCollection (GeneralCollection $atendimento_formulario_collection): bool {
        $sql = 'insert into formulario_atendimento (
                    `id_formulario`,
                    `criado_em`,
                    `atualizado_em`
                ) values ();
        ';
        $values = $atendimento_formulario_collection->map(function (AtendimentoFormulario $atendimento_formulario) : string {
            $today = DateUtils::today();
            $sql = "(
                {$atendimento_formulario->getIdFormulario()}, 
                '{$today}', 
                '{$today}' 
            )";
            return $sql;
        })->join(',');
        $sql = str_replace('values ()', "values $values;", $sql);
        $query = $this->db->query($sql);
        return $query->connID->affected_rows === $atendimento_formulario_collection->length;
    }
}
