<?php

namespace App\Models;

use App\Entities\ProfissionalClinica\ProfissionalClinica;
use CodeIgniter\Model;

class ProfissionalClinicaModel extends Model {
	protected $table = 'profissional_clinica';
	protected $primaryKey = 'id';
	protected $returnType = 'array';
	protected $allowedFields = [ 
		'id_profissional',
		'id_pessoa_juridica' ];
	protected $useSoftDeletes = false;
	protected $skipValidation = true;
	protected $useTimestamps = false;

	public function registerProfissionalClinica(ProfissionalClinica &$profissional_clinica): void {

		$profissional_data = $profissional_clinica->toArray();
		unset( $profissional_data ['id'] );
		$id_new_profissional = $this->insert( $profissional_data );
		$profissional_clinica->setId( $id_new_profissional );

	}

	public function updateProfissionalClinicaByAdministrativoID($id_administrativo, ProfissionalClinica $profissional_clinica) {

		$data = $profissional_clinica->toArray( true );
		if (count( $data ) > 0) {
			$binds = [ 
				'id_administrativo' => $id_administrativo ];
			$id_clinica = $profissional_clinica->getIdPessoaJuridica();
			$sql = "
                UPDATE profissional_clinica
                SET id_pessoa_juridica = $id_clinica
                WHERE id_profissional in (select id_profissional from administrativo a where a.id = :id_administrativo:)
            ";
			$query = $this->db->query( $sql, $binds );
			$errors = $this->errors();
			return !$errors;
		} else {
			return true;
		}

	}

}
