<?php

namespace App\DAO;

use App\DAO\DAO;
use App\DAO\DAOResponse\DAOResponse;
use App\ViewModels\Anamnese\AnamneseAdicionalRegistrationViewModel\AnamneseAdicionalRegistrationViewModel;
use App\Services\RequisicaoService;
use App\Services\AgendaService;
use App\Services\AgendamentoService;
use Exception;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Services\TipoProcedimentoService;
use App\Services\PetService;
use App\Utils\DateUtils;

class AnamneseDAO extends DAO {

	public function newAnamneseAdicional(AnamneseAdicionalRegistrationViewModel $objRegister): DAOResponse {

		$dao_response = new DAOResponse();
		try {
			$requisicaoService = new RequisicaoService();
			$agendaService = new AgendaService();
			$agendamentoService = new AgendamentoService();
			$tipoprocedimentoService = new TipoProcedimentoService();
			$petService = new PetService();

			//$this->db->transStart();
			
			if($objRegister->getIdMedico() > 0) {
				$requisicao_response = $requisicaoService->registerRequisicaoATDContinuado( $objRegister );
			}
			
			if($objRegister->getIdAssistente() > 0) {
				$requisicao_response = $requisicaoService->registerRequisicaoEXContinuado( $objRegister );
			}
			

			if ($requisicao_response->getStatus() == ServiceResponseInterface::SUCESS) {
				$requisicao_data = $requisicao_response->getData();
				$objRegister->setIdRequisicao( $requisicao_data ['id_requisicao'] );
				$tipoprocedimentoService_response = $tipoprocedimentoService->getTipoProcedimentoByProcedimentoId( $objRegister->getIdProcedimento() );

				if ($tipoprocedimentoService_response->getStatus() == ServiceResponseInterface::SUCESS) {
					$agenda_array = $this->getAgendaArray( $objRegister, $tipoprocedimentoService_response->getData() );
					$agendaService_response = $agendaService->registerAgenda( $agenda_array );

					if ($agendaService_response->getStatus() == ServiceResponseInterface::SUCESS) {
						$petService_response = $petService->getTutorIdsByPetId( $objRegister->getIdPet() );

						if ($petService_response->getStatus() == ServiceResponseInterface::SUCESS) {
							$agendamento_array = $this->getAgendamentoArray( $objRegister, $agendaService_response->getData(), $petService_response->getData() );
							$agendamentoService_response = $agendamentoService->registerAgendamentoAnamneseAdcional( $agendamento_array );

							if ($agendamentoService_response->getStatus() == ServiceResponseInterface::SUCESS) {
								$dao_response->setStatus( $agendamentoService_response->getStatus() );
								$dao_response->setData( 
									[ 
										'agendamento' => $agendamentoService_response->getData(),
										'agenda' => $agendaService_response->getData(),
										'requisicao' => $requisicao_response->getData()
									] );
								//$this->db->transComplete();
							} else {
								$dao_response->setStatus( $agendamentoService_response->getStatus() );
								$dao_response->setErrors( $agendamentoService_response->getErrors() );
								$dao_response->setData( [ ] );
							}
						} else {
							$dao_response->setStatus( $petService_response->getStatus() );
							$dao_response->setErrors( $petService_response->getErrors() );
							$dao_response->setData( [ ] );
						}
					} else {
						$dao_response->setStatus( $agendaService_response->getStatus() );
						$dao_response->setErrors( $agendaService_response->getErrors() );
						$dao_response->setData( [ ] );
					}
				} else {
					$dao_response->setStatus( $tipoprocedimentoService_response->getStatus() );
					$dao_response->setErrors( $tipoprocedimentoService_response->getErrors() );
					$dao_response->setData( [ ] );
				}
			} else {
				$dao_response->setStatus( $requisicao_response->getStatus() );
				$dao_response->setErrors( $requisicao_response->getErrors() );
				$dao_response->setData( [ ] );
			}
		} catch ( Exception $e ) {
			$dao_response->setStatus( DAOResponseInterface::UNKNOWN_ERROR );
			$dao_response->setErrors( $e );
			$dao_response->setData( [ ] );
		}

		return $dao_response;

	}

	private function getAgendaArray(AnamneseAdicionalRegistrationViewModel $objRegister, array $objTipoPorcedimento): array {

		$hr_fim = date( 'H:i:s', strtotime( "+1 minutes" ) );

		$array_resp = [ 
			'data' => $objRegister->getDataCriacao(),
			'hora_inicio' => $objRegister->getHoraCriacao(),
			'hora_final' => $hr_fim,
			'id_medico' => $objRegister->getIdMedico(),
			'id_assistente' => $objRegister->getIdAssistente(),
			'id_unidade' => $objRegister->getIdUnidade(),
			'id_tipo_procedimento' => $objTipoPorcedimento [0] ['tipo_id'],
			'id_requisicao' => $objRegister->getIdRequisicao()
		];

		return $array_resp;

	}

	private function getAgendamentoArray(AnamneseAdicionalRegistrationViewModel $objRegister, array $objAgenda, array $objTutorIds): array {

		$array_resp = [ 
			'id_agenda' => $objAgenda ['id_agenda'],
			'id_cliente' => $objTutorIds [0] ['id_cliente'],
			'id_paciente' => $objTutorIds [0] ['id_paciente'],
			'id_procedimento' => $objRegister->getIdProcedimento(),
			'id_pet' => $objRegister->getIdPet(),
			'id_requisicao' => $objRegister->getIdRequisicao()
		];

		return $array_resp;

	}

}

