<?php


namespace App\Models;


use App\Entities\RespostaFormulario\RespostaFormulario;
use App\Utils\Collections\GeneralCollection;

class RespostaFormularioModel extends ModelEntity
{
    protected $table = 'resposta_formulario';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $allowedFields = [
        'id_pergunta',
        'id_consulta',
    ];
    protected $useSoftDeletes = false;
    protected $skipValidation = true;
    protected $useTimestamps = false;

    public function registerRespostaFormulario (RespostaFormulario $resposta_formulario): void {
        $resposta_formulario_data = $resposta_formulario->toArray(true);
        $id_resposta_formulario = $this->insert($resposta_formulario_data);
        $resposta_formulario->setId($id_resposta_formulario ?? 0);
    }

    public function registerRespostaFormularioCollection (GeneralCollection $resposta_formulario_collection): bool {
        $sql = 'insert into opcao_pergunta (
                    `id_pergunta`,
                    `id_consulta`
                ) values ();
        ';
        $values = $resposta_formulario_collection->map(function (RespostaFormulario $resposta_formulario) : string {
            $sql = "(
                {$resposta_formulario->getIdPERGUNTA()},   
                {$resposta_formulario->getIdCONSULTA()}   
            )";
            return $sql;
        })->join(',');
        $sql = str_replace('values ()', "values $values;", $sql);
        $query = $this->db->query($sql);
        return $query->connID->affected_rows === $resposta_formulario_collection->length;
    }
}
