<?php


namespace App\Models;

use App\Entities\Prontuario\Prontuario;

class ProntuarioModel extends ModelEntity {
	protected $table = 'prontuario';
	protected $primaryKey = 'id';
	protected $returnType = 'array';
	protected $allowedFields = [ 
		'adicionado_por',
		'alterado_por',
		'id_medico',
		'id_paciente'
	];
	protected $useSoftDeletes = false;
	protected $skipValidation = true;
	protected $useTimestamps = true;
	protected $createdField = 'criado_em';
	protected $updatedField = 'alterado_em';

	public function registerProntuario(Prontuario $prontuario): void {

		$prontuario_data = $prontuario->toArray( true );
		$id_new_prontuario = $this->insert( $prontuario_data );
		$prontuario->setId( $id_new_prontuario );

	}

	public function updateProntuarioByID(int $id_consulta, Prontuario $endereco): bool {

		$data = $endereco->toArray( true );
		if (count( $data ) > 0) {
			$binds = [ 
				'id_consulta' => $id_consulta
			];
			$sql = "
                UPDATE prontuario
            ";
			$sql .= $this->generateUpdateSets( $data, $binds );
			$sql .= 'WHERE prontuario.id = :id_consulta:';
			$query = $this->db->query( $sql, $binds );
			$errors = $this->errors();
			return !$errors;
		} else {
			return true;
		}

	}

}
