<?php


namespace App\Models;

use App\Entities\Medico\Medico;

class MedicoModel extends ModelEntity {
	protected $table = 'medico';
	protected $primaryKey = 'id';
	protected $returnType = 'array';
	protected $allowedFields = [ 
		'id_profissional',
		'assinatura',
		'documento'
	];
	protected $useSoftDeletes = false;
	protected $skipValidation = true;
	protected $useTimestamps = false;

	public function registerMedico(Medico &$medico): void {

		$medico_data = $medico->toArray();
		unset( $medico_data ['id'] );
		$id_new_assistente = $this->insert( $medico_data );
		$medico->setId( $id_new_assistente );

	}

	public function updateMedicoByID(int $id_paciente, Medico $medico): bool {

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

	}

	public function updateMedicoByMedicoID(int $id_medico, Medico $medico): bool {

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

	}

}
