<?php


namespace App\Models;

use App\Entities\Formulario\Formulario;
use App\Entities\OpcaoPergunta\OpcaoPergunta;
use App\Entities\Paciente\Paciente;
use App\Entities\Pergunta\Pergunta;
use App\Services\PacienteService;
use App\Services\PessoaService;
use App\Services\Service;
use App\Utils\Collections\GeneralCollection;
use App\Utils\ModelUtils;
use App\ViewModels\Formulario\FormularioRegistrationViewModel\PerguntaRegistration;
use App\ViewModels\Paciente\PacienteRegistrationViewModel\PacienteRegistrationViewModel;
use CodeIgniter\Model;

class OpcaoPerguntaModel extends ModelEntity {
	protected $table = 'pergunta';
	protected $primaryKey = 'id';
	protected $returnType = 'array';
	protected $allowedFields = [ 
			'texto_pergunta',
			'id_tipo_pergunta',
			'obrigatorio',
			'id_formulario'
	];
	protected $useSoftDeletes = false;
	protected $skipValidation = true;
	protected $useTimestamps = false;

	public function registerOpcaoPerguntaCollection(GeneralCollection $opcao_pergunta_collection): bool {

		$sql = 'insert into opcao_pergunta (
                    `id_pergunta`,
                    `descricao`
                ) values ();
        ';
		$values = $opcao_pergunta_collection->map( 
				function (OpcaoPergunta $opcao_pergunta): string {
					$sql = "(
                {$opcao_pergunta->getIdPERGUNTA()},   
                '{$opcao_pergunta->getDescricao()}'   
            )";
					return $sql;
				} )
			->join( ',' );
		$sql = str_replace( 'values ()', "values $values;", $sql );
		log_message( 'info', $sql );
		$query = $this->db->query( $sql );
		return $query->connID->affected_rows === $opcao_pergunta_collection->length;

	}

}
