<?php

namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\Filter\QueryFilter;
use App\DAO\Pagination\Pagination;
use App\DAO\ProcedimentoDAO;
use App\Entities\Procedimento\Procedimento;
use App\Mappers\ProcedimentoMapper;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;

class ProcedimentoService extends Service {

	public function registerProcedimento(array $data_request): ServiceResponse {

		$procedimento_registration_vm = ProcedimentoMapper::getProcedimentoRegistrationVMFromArray( $data_request );
		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->persistProcedimentoRegistrationVM( $procedimento_registration_vm );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$data = [ 
					'id_procedimento' => $dao_response->getData()
						->getId()
				];
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $data );
				break;
			case DAOResponseInterface::FOREIGN_KEY_DONT_EXISTS :
			case DAOResponseInterface::DUPLICATED_UNIQUE_KEY :
				$service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				break;
		}
		return $service_response;

	}

	public function getProcedimentoList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection, ?QueryOrderCollection $query_order_collection): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->getProcedimentoList( $pagination, $query_filter_collection, $query_order_collection );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$dao_response_data = $dao_response->getData();
			$procedimento_details_array = ProcedimentoMapper::getProcedimentosDetailsArrayFromProcedimentoDetailsVMCollection( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
				'procedimentos' => $procedimento_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}

	public function getProcedimentoConsultaList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection, $unidade_id): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->getProcedimentoConsultaList( $pagination, $query_filter_collection, $query_order_collection, $unidade_id );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$dao_response_data = $dao_response->getData();
			$procedimento_details_array = ProcedimentoMapper::getProcedimentosDetailsArrayFromProcedimentoDetailsVMCollection( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
				'procedimentos' => $procedimento_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}

	public function getProcedimentoExameList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection, $unidade_id): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->getProcedimentoExameList( $pagination, $query_filter_collection, $query_order_collection, $unidade_id );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$dao_response_data = $dao_response->getData();
			$procedimento_details_array = ProcedimentoMapper::getProcedimentosDetailsArrayFromProcedimentoDetailsVMCollection( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
				'procedimentos' => $procedimento_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}
	
	public function getProcedimentoHigieneList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
	    ?QueryOrderCollection $query_order_collection, $unidade_id): ServiceResponse {
	        
	        $procedimento_dao = new ProcedimentoDAO();
	        $dao_response = $procedimento_dao->getProcedimentoHigieneList( $pagination, $query_filter_collection, $query_order_collection, $unidade_id );
	        switch ($dao_response->getStatus()) {
	            case DAOResponseInterface::SUCESS :
	                $service_response_status = ServiceResponseInterface::SUCESS;
	                break;
	            case DAOResponseInterface::RECORD_NOT_FOUND :
	                $service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
	                break;
	            case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
	                $service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
	                break;
	        }
	        $service_response = ServiceResponse::builder()->setStatus( $service_response_status )
	        ->build();
	        if ($service_response_status === DAOResponseInterface::SUCESS) {
	            $dao_response_data = $dao_response->getData();
	            $procedimento_details_array = ProcedimentoMapper::getProcedimentosDetailsArrayFromProcedimentoDetailsVMCollection( $dao_response_data );
	            $pagination = $dao_response->getPagination();
	            $data = [
	                'procedimentos' => $procedimento_details_array,
	                'pagination' => $pagination ? $pagination->toResponseArray() : null
	            ];
	            $service_response->setData( $data );
	        }
	        return $service_response;
	        
	}

	public function getProcedimentoInternacaoList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection, $unidade_id): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->getProcedimentoInternacaoList( $pagination, $query_filter_collection, $query_order_collection, $unidade_id );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$dao_response_data = $dao_response->getData();
			$procedimento_details_array = ProcedimentoMapper::getProcedimentosDetailsArrayFromProcedimentoDetailsVMCollection( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
				'procedimentos' => $procedimento_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}

	public function getProcedimentoMedicacaoList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection, $unidade_id): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->getProcedimentoMedicacaoList( $pagination, $query_filter_collection, $query_order_collection, $unidade_id );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$dao_response_data = $dao_response->getData();
			$procedimento_details_array = ProcedimentoMapper::getProcedimentosDetailsArrayFromProcedimentoDetailsVMCollection( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
				'procedimentos' => $procedimento_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}

	public function getProcedimentoCirurgiaList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection, $unidade_id): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->getProcedimentoCirurgiaList( $pagination, $query_filter_collection, $query_order_collection, $unidade_id );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$dao_response_data = $dao_response->getData();
			$procedimento_details_array = ProcedimentoMapper::getProcedimentosDetailsArrayFromProcedimentoDetailsVMCollection( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
				'procedimentos' => $procedimento_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}

	public function getProcedimento(QueryFilter $query_filter): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$query_filter_collection = new QueryFilterCollection();
		$query_filter_collection->push( $query_filter );
		$dao_response = $procedimento_dao->getṔrocedimento( $query_filter_collection );
		$service_response_status = [ 
			DAOResponseInterface::SUCESS => ServiceResponseInterface::SUCESS,
			DAOResponseInterface::RECORD_NOT_FOUND => ServiceResponseInterface::RECORD_NOT_FOUND,
			DAOResponseInterface::DATABASE_CONNECTION_ERROR => ServiceResponseInterface::DATABASE_CONNECTION_ERROR
		] [$dao_response->getStatus()];
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status ?? ServiceResponseInterface::UNKNOWN_ERROR)
			->build();
		if ($service_response_status === ServiceResponseInterface::SUCESS) {
			$service_response->setData( $dao_response->getData()
				->toArray() );
		}
		return $service_response;

	}

	public function deleteProcedimento(int $id_procedimento): ServiceResponse {

		$procedimento_dao = new ProcedimentoDAO();
		$dao_response = $procedimento_dao->deleteProcedimento( $id_procedimento );
		$service_response_status = [ 
			DAOResponseInterface::RESTRICT_FK_UPDATE_OR_DELETION => ServiceResponseInterface::RECORD_IS_LINKED,
			DAOResponseInterface::SUCESS => ServiceResponseInterface::SUCESS,
			DAOResponseInterface::RECORD_NOT_FOUND => ServiceResponseInterface::RECORD_NOT_FOUND
		] [$dao_response->getStatus()];

		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		return $service_response;

	}

	public function updateProcedimentoData(int $id_paciente, array $data): ServiceResponse {

		$paciente_update_vm = ProcedimentoMapper::getProcedimentoUpdateVMFromArray( $data );
		$paciente_dao = new ProcedimentoDAO();
		$dao_response = $paciente_dao->persistProcedimentoUpdateVM( $id_paciente, $paciente_update_vm );
		$service_response = new ServiceResponse();
		$response_status = [ 
			DAOResponseInterface::SUCESS => ServiceResponseInterface::SUCESS,
			DAOResponseInterface::RECORD_NOT_FOUND => ServiceResponseInterface::RECORD_NOT_FOUND,
			DAOResponseInterface::DATABASE_CONNECTION_ERROR => ServiceResponseInterface::DATABASE_CONNECTION_ERROR,
			DAOResponseInterface::FOREIGN_KEY_DONT_EXISTS => ServiceResponseInterface::VALIDATION_PARAMS_ERROR,
			DAOResponseInterface::DUPLICATED_UNIQUE_KEY => ServiceResponseInterface::VALIDATION_PARAMS_ERROR
		] [$dao_response->getStatus()];
		if (!$response_status) {
			(new LogService())->log_error( $response_status, "Ocorreu um erro desconhecido durante a atualização o procedimento", [ ] );
			$response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
		} else if ($response_status !== ServiceResponseInterface::SUCESS) {
			$service_response->setErrors( $dao_response->getErrors() );
		}
		$service_response->setStatus( $response_status );
		return $service_response;

	}

	public function getProcedimentoByUnidadeByTipoConvenioId(int $unidadeId, int $tipoConvenio): ServiceResponse {

		$dao = new ProcedimentoDAO();
		$daoResponse = $dao->getProcedimentoByUnidadeByTipoConvenioId( $unidadeId, $tipoConvenio );

		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;

	}
	
	public function getProcedimentoByUnidadeByTipoId(int $unidadeId, int $tipoProcedimento):ServiceResponse{
		$dao = new ProcedimentoDAO();
		$daoResponse = $dao->getProcedimentoByUnidadeByTipoId( $unidadeId, $tipoProcedimento );
		
		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $daoResponse );
		return $service_response;
		
	}

}