<?php

namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\Filter\QueryFilter;
use App\DAO\Pagination\Pagination;
use App\DAO\TipoProcedimentoDAO;
use App\Mappers\TipoProcedimentoMapper;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\GeneralCollection;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\Utils\ResponseException;

class TipoProcedimentoService extends Service {

	public function registerTipoProcedimento(array $data): ServiceResponse {

		$tipo_procedimento_registration_vm = TipoProcedimentoMapper::getTipoProcedimentoRegistrationViewModelFromArray( $data );
		$tipo_procedimento_dao = new TipoProcedimentoDAO();
		$dao_response = $tipo_procedimento_dao->persistTipoProcedimentoRegistrationVM( $tipo_procedimento_registration_vm );
		$service_response = ServiceResponse::builder()->build();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$data = [ 
					'id_tipo_procedimento' => $dao_response->getData()
						->getId()
				];
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $data );
				break;
			case DAOResponseInterface::DUPLICATED_UNIQUE_KEY :
				$service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
				$service_response->setErrors( [ 
					'descricao' => 'Esse tipo de procedimento já existe.'
				] );
				break;
		}
		return $service_response;

	}

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

		$tipo_procedimento_dao = new TipoProcedimentoDAO();
		$dao_response = $tipo_procedimento_dao->getTipoProcedimentoList( $pagination, $query_filter_collection, $query_order_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 )
			->build();
		if ($service_response_status === ServiceResponseInterface::SUCESS) {
			$tipo_procedimento_details_vm_collection = (function (GeneralCollection $collection): GeneralCollection {
				return $collection;
			})( $dao_response->getData() );
			$pagination = $dao_response->getPagination();
			$tipo_procedimento_details_array = $tipo_procedimento_details_vm_collection->toArray();

			$data = [ 
				'tipo_procedimentos' => $tipo_procedimento_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];

			// $service_response->setData($tipo_procedimento_details_array);

			$service_response->setData( $data );
		}
		return $service_response;

	}

	public function deleteTipoProcedimento(int $id_procedimento): ServiceResponse {

		$tipo_procedimento_dao = new TipoProcedimentoDAO();
		$dao_response = $tipo_procedimento_dao->deleteTipoProcedimento( $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 getTipoProcedimento(QueryFilter $query_filter): ServiceResponse {

		$especie_dao = new TipoProcedimentoDAO();
		$query_filter_collection = new QueryFilterCollection();
		$query_filter_collection->push( $query_filter );
		$dao_response = $especie_dao->getTipoProcedimento( $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 updateTipoProcedimentoData(int $id_paciente, array $data): ServiceResponse {

		$paciente_update_vm = TipoProcedimentoMapper::getTipoProcedimentoUpdateVMFromArray( $data );
		$paciente_dao = new TipoProcedimentoDAO();
		$dao_response = $paciente_dao->persistTipoProcedimentoUpdateVM( $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 especie", [ ] );
			$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 getTipoProcedimentoByProcedimentoId(int $procedimentoId): ServiceResponse {
		
		$dao = new TipoProcedimentoDAO();
		$dao_response = $dao->getTipoProcedimentoByProcedimentoId($procedimentoId);
		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $dao_response );
		return $service_response;


	}
	
	public function getTipoProcedimentoByRequisicaoId(int $requisicaoId):ServiceResponse{
		$dao = new TipoProcedimentoDAO();
		$dao_response = $dao->getTipoProcedimentoByRequisicaoId($requisicaoId);
		$service_response = new ServiceResponse();
		$service_response = $this->setServiceResponse( $dao_response );
		return $service_response;
		
	}

}