<?php


namespace App\Models;

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

class ProcedimentoFormularioModel extends Model
{

    protected $table = 'procedimento_formulario';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $allowedFields = [
        'id_formulario',
        'id_procedimento',
    ];
    protected $useSoftDeletes = false;
    protected $skipValidation = true;
    protected $useTimestamps = false;

    public function registerOpcaoPerguntaCollection (GeneralCollection $procedimento_formulario_collection): bool {
        $sql = 'insert into procedimento_formulario (
                    `id_formulario`,
                    `id_procedimento`
                ) values ();
        ';
        $values = $procedimento_formulario_collection->map(function (ProcedimentoFormulario $procedimento_formulario) : string {
            $sql = "(
                {$procedimento_formulario->getIdFORMULARIO()},   
                '{$procedimento_formulario->getIdPROCEDIMENTO()}'   
            )";
            return $sql;
        })->join(',');
        $sql = str_replace('values ()', "values $values;", $sql);
        $query = $this->db->query($sql);
        return $query->connID->affected_rows === $procedimento_formulario_collection->length;
    }
}
