<?php

namespace App\Services;

use App\Entities\Profissional\Profissional;
use App\Entities\Pessoa\Pessoa;
use App\Models\ProfissionalModel;
use App\Services\ServiceResponse\ServiceResponse;
use App\DAO\ProfissionalDAO;
use Exception;

class ProfissionalService extends Service {

	public function registerProfissional(array $data): array {

		try {
			$pessoa_service = new PessoaService();
			$pessoa = $pessoa_service->getPessoaFromData( $data );
			$profissional = $this->getProfissionalFromPessoa( $pessoa );
			$sucess_register = $profissional->register();
			$response = [ 
				'status' => $sucess_register ? SUCESS : DATABASE_CONNECTION_ERROR
			];
		} catch ( Exception $exception ) {
			log_message( 'error', $exception->getMessage() );
			$response = [ 
				'status' => $exception->getCode()
			];
		}
		return $response;

	}

	public function getProfissionalFromID(int $id_profissional): ?array {

		$profissional_model = new ProfissionalModel();
		$profissional_detalhes_view_model = $profissional_model->getProfissionalDataByID( $id_profissional );
		$response = [ ];
		$status = $profissional_detalhes_view_model ? SUCESS : RECORD_NOT_FOUND;
		if (isset( $profissional_detalhes_view_model )) {
			$profissional_detalhes = $profissional_detalhes_view_model->toArray();
			$response ['data'] = [ 
				'profissional' => $profissional_detalhes
			];
		}
		$response ['status'] = $status;
		return $response;

	}

	public function getProfissionalAll(array $request) {

		$profissionais = Profissional::getProfissionalsAll( $request );
		if ($profissionais) {
			return [ 
				'status' => SUCESS,
				'data' => $profissionais
			];
		} else {
			return [ 
				'status' => RECORD_NOT_FOUND,
				'data' => null
			];
		}

	}

	public function getProfissionalFromPessoa(Pessoa $pessoa, array $data = null): Profissional {

		$profissional = Profissional::builder()->setId( $data ['id'] )
			->setPessoa( $pessoa )
			->build();
		return $profissional;

	}

	public function getIdProfissionalByMedicoId(int $idMedico): ServiceResponse {

		$dao = new ProfissionalDAO();
		$dao_response = $dao->getIdProfissionalByMedicoId( $idMedico );
		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $dao_response );
		return $service_response;

	}

	public function getIdProfissionalByAssistenteId(int $idAssistente): ServiceResponse {

		$dao = new ProfissionalDAO();
		$dao_response = $dao->getIdProfissionalByAssistenteId( $idAssistente );
		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $dao_response );
		return $service_response;

	}

	public function getIdProfissionalByAdministrativoId(int $idAdministrativo): ServiceResponse {

		$dao = new ProfissionalDAO();
		$dao_response = $dao->getIdProfissionalByAdministrativoId( $idAdministrativo );
		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $dao_response );
		return $service_response;

	}

	public function getIdProfissionalByUsuarioId(int $idUsuario): ServiceResponse {

		$dao = new ProfissionalDAO();
		$dao_response = $dao->getIdProfissionalByUsuarioId( $idUsuario );
		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $dao_response );
		return $service_response;

	}

}