<?php

namespace App\Services;

use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\DAO\ComandaDAO;
use App\DAO\ConsultaDAO;
use App\DAO\AssistenteDAO;
use App\DAO\RequisicaoDAO;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;

class AuditComandaService extends Service {

	public function getItensByComandaId(int $comandaId): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ComandaDAO();
		$daoResponse = $dao->getItensByComandaIdForAudit( $comandaId );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}

	public function getProntuarioByComandaId(int $comandaId): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ConsultaDAO();
		$daoResponse = $dao->getConsultaListByComandaID( $comandaId );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}

	public function getAssistenteAviableToRequisicao(int $requisicaoId): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new AssistenteDAO();
		$daoResponse = $dao->getAssistenteAviableToRequisicao( $requisicaoId );
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}

	public function adminExecExame(array $request): ServiceResponse {

		$data_exec = $request ['data_exec'];
		$hora_exec = $request ['hora_exec'];
		$requisicao_id = $request ['requisicao_id'];
		$assistente_id = $request ['assistente_id'];

		$service_response = new ServiceResponse();
		$dao = new RequisicaoDAO();

		$daoResponse = $dao->adminExecExame( $requisicao_id, $assistente_id, $data_exec, $hora_exec );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	public function adminExcluirExame(array $request): ServiceResponse {

		$service_response = new ServiceResponse();
		$requisicao_id = $request ['requisicao_id'];
		$req_service = new RequisicaoService();
		$req_response = $req_service->getStatusRequisicao( $requisicao_id );
		if ($req_response->getStatus() == ServiceResponseInterface::SUCESS) {
			$req_status = $req_response->getData();
			if ($req_status ['id_status'] != 1) {
				$daoResponse = new DAOResponse();
				$daoResponse->setStatus( DAOResponseInterface::INSUFFICIENT_DATA );
				$service_response = $this->setServiceResponse( $daoResponse );
				return $service_response;
			}
		}
		if ($req_response->getStatus() != ServiceResponseInterface::SUCESS) {
			$req_status = $req_response->getData();
			if ($req_status ['status_id'] != 1) {
				$daoResponse = new DAOResponse();
				$daoResponse->setStatus( $req_response->getStatus() );
				$daoResponse->setErrors( $req_response->getErrors() );
				$service_response = $this->setServiceResponse( $daoResponse );
				return $service_response;
			}
		}

		$req_excluir_response = $req_service->adminExcluirExame( $requisicao_id );

		if ($req_excluir_response->getStatus() != ServiceResponseInterface::SUCESS) {
			return $req_excluir_response;
		}

		$dao_comanda = new ComandaDAO();
		$dao_comanda_response = $dao_comanda->adminExcluirItemComandaPublicaByRequisicaoID( $requisicao_id );

		$service_response = $this->setServiceResponse( $dao_comanda_response );
		return $service_response;

	}
	
	public function adminExcluirConsulta(array $request): ServiceResponse {
		
		$service_response = new ServiceResponse();
		$requisicao_id = $request ['requisicao_id'];
		$req_service = new RequisicaoService();
		$req_response = $req_service->getStatusRequisicao( $requisicao_id );
		if ($req_response->getStatus() == ServiceResponseInterface::SUCESS) {
			$req_status = $req_response->getData();
			if ($req_status ['id_status'] != 1) {
				$daoResponse = new DAOResponse();
				$daoResponse->setStatus( DAOResponseInterface::INSUFFICIENT_DATA );
				$service_response = $this->setServiceResponse( $daoResponse );
				return $service_response;
			}
		}
		if ($req_response->getStatus() != ServiceResponseInterface::SUCESS) {
			$req_status = $req_response->getData();
			if ($req_status ['status_id'] != 1) {
				$daoResponse = new DAOResponse();
				$daoResponse->setStatus( $req_response->getStatus() );
				$daoResponse->setErrors( $req_response->getErrors() );
				$service_response = $this->setServiceResponse( $daoResponse );
				return $service_response;
			}
		}
		
		$req_excluir_response = $req_service->adminExcluirConsulta( $requisicao_id );
		
		if ($req_excluir_response->getStatus() != ServiceResponseInterface::SUCESS) {
			return $req_excluir_response;
		}
		
		$dao_comanda = new ComandaDAO();
		$dao_comanda_response = $dao_comanda->adminExcluirItemComandaPublicaByRequisicaoID( $requisicao_id );
		
		$service_response = $this->setServiceResponse( $dao_comanda_response );
		return $service_response;
		
	}

	public function adminNewReqExecExame(array $request): ServiceResponse {

		$service_response = new ServiceResponse();

		$cp_service = new ConvenioProcedimentoService();
		$cp_service_response = $cp_service->getDadosConvenioProcedimentoID( $request ['id_convenio_procedimento'] );

		if ($cp_service_response->getStatus() != ServiceResponseInterface::SUCESS) {
			$daoResponse = new DAOResponse();
			$daoResponse->setStatus( $cp_service_response->getStatus() );
			$daoResponse->setErrors( $cp_service_response->getErrors() );
			$service_response = $this->setServiceResponse( $daoResponse );
			return $service_response;
		}

		$objConvenioProcedimento = $cp_service_response->getData();

		// recuperar id_paciente .

		$paciente_service = new PacienteService();
		$paciente_service_response = $paciente_service->getDadosPacienteByIdCiente( $request ['id_cliente'] );
		if ($paciente_service_response->getStatus() != ServiceResponseInterface::SUCESS) {
			$daoResponse = new DAOResponse();
			$daoResponse->setStatus( $paciente_service_response->getStatus() );
			$daoResponse->setErrors( $paciente_service_response->getErrors() );
			$service_response = $this->setServiceResponse( $daoResponse );
			return $service_response;
		}
		$objPaciente = $paciente_service_response->getData();
		$idPaciente = $objPaciente ['id_paciente'];

		$req_service = new RequisicaoService();
		$req_response = $req_service->adminNewReqExame( $request );

		if ($req_response->getStatus() != ServiceResponseInterface::SUCESS) {
			$daoResponse = new DAOResponse();
			$daoResponse->setStatus( $req_response->getStatus() );
			$daoResponse->setErrors( $req_response->getErrors() );
			$service_response = $this->setServiceResponse( $daoResponse );
			return $service_response;
		}

		$req_resp = $req_response->getData();
		$idRequisicao = $req_resp ['id_requisicao'];

		$agenda_array = array ();
		$horaFinal = date( 'H:i:s', strtotime( $request ['hora_exec'] . ' +1 minutes' ) );
		$agenda_array = [ 
			'data' => $request ['data_exec'],
			'hora_inicio' => $request ['hora_exec'],
			'hora_final' => $horaFinal,
			'id_medico' => 0,
			'id_assistente' => $request ['assistente_id'],
			'id_unidade' => $objConvenioProcedimento [0] ['id_unidade'],
			'id_tipo_procedimento' => $objConvenioProcedimento [0] ['id_tipo_procedimento']
		];

		$agenda_service = new AgendaService();
		$agenda_response = $agenda_service->registerAgenda( $agenda_array );

		if ($agenda_response->getStatus() != ServiceResponseInterface::SUCESS) {
			// desfazer requisicao
			$this->undoNewRequisicao( $idRequisicao );
			$daoResponse = new DAOResponse();
			$daoResponse->setStatus( $agenda_response->getStatus() );
			$daoResponse->setErrors( $agenda_response->getErrors() );
			$service_response = $this->setServiceResponse( $daoResponse );

			return $service_response;
		}

		$resp_agenda = $agenda_response->getData();
		$idAgenda = $resp_agenda ['id_agenda'];

		$agendamento_array = array ();
		$agendamento_array = [ 
			'id_agenda' => $idAgenda,
			'id_cliente' => $request ['id_cliente'],
			'id_paciente' => $idPaciente,
			'id_procedimento' => $objConvenioProcedimento [0] ['id_procedimento'],
			'id_requisicao' => $idRequisicao,
			'id_pet' => $request ['id_pet']
		];
		$agendamento_service = new AgendamentoService();
		$agendamento_response = $agendamento_service->registerAgendamentoAnamneseAdcional( $agendamento_array );
		
		if ($agendamento_response->getStatus() != ServiceResponseInterface::SUCESS) {
			// desfafer agenda
			$this->undoNewAgenda( $idAgenda );
			// desfazer requisicao
			$this->undoNewRequisicao( $idRequisicao );
			$daoResponse = new DAOResponse();
			$daoResponse->setStatus( $agendamento_response->getStatus() );
			$daoResponse->setErrors( $agendamento_response->getErrors() );
			$service_response = $this->setServiceResponse( $daoResponse );
			// desfafer agenda , requisicao
			return $service_response;
		}
		 // registrar consulta
		$resp_agendamento = $agendamento_response->getData();
		$idAgendamento = $resp_agendamento['id_agendamento'];
		
		$consulta_array = array();
		
		$consulta_array =['id_agendamento' =>$idAgendamento ];
		$consulta_service = new ConsultaService();
		$consulta_response = $consulta_service->auditRegisterExameMedicacaoAtendimento($consulta_array);
		
		if ($consulta_response->getStatus() != ServiceResponseInterface::SUCESS) {
			//desfazer agendamento
			$this->undoNewAgendamento( $idAgendamento );
			// desfafer agenda
			$this->undoNewAgenda( $idAgenda );
			// desfazer requisicao
			$this->undoNewRequisicao( $idRequisicao );
			$daoResponse = new DAOResponse();
			$daoResponse->setStatus( $agendamento_response->getStatus() );
			$daoResponse->setErrors( $agendamento_response->getErrors() );
			$service_response = $this->setServiceResponse( $daoResponse );
			// desfafer agenda , requisicao
			return $service_response;
		}
		
		$resp_consulta = $consulta_response->getData();
		$idConsulta = $resp_consulta['id_consulta'];
		
		// atualizar requisicao 
		$req_response = $req_service->adminUpdateConsultaIdNewReqExame($idRequisicao, $idConsulta);
		
		$dao_comanda = new ComandaDAO();
		$dao_comanda_response = $dao_comanda->adminNewReqExecExameInComanda( $request, $objConvenioProcedimento, $idRequisicao );

		if ($dao_comanda_response->getStatus() != ServiceResponseInterface::SUCESS) {
			// desfazer consulta
			$this->undoNewConsuta($idConsulta);
			// desfafer agendamento
			$this->undoNewAgendamento( $idAgendamento );
			// desfafer agenda
			$this->undoNewAgenda( $idAgenda );
			// desfazer requisicao
			$this->undoNewRequisicao( $idRequisicao );
			$daoResponse = new DAOResponse();
			$daoResponse->setStatus( $dao_comanda_response->getStatus() );
			$daoResponse->setErrors( $dao_comanda_response->getErrors() );
			$service_response = $this->setServiceResponse( $daoResponse );

			return $service_response;
		}

		$service_response = $this->setServiceResponse( $dao_comanda_response );
		return $service_response;

	}
	
	private function undoNewConsuta(int $consulta_id){
		
	}

	private function undoNewAgendamento(int $agendamento_id) {


	}

	private function undoNewAgenda(int $agenda_id) {


	}

	private function undoNewRequisicao(int $requisicao_id) {


	}

}

