<?php

namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\Filter\QueryFilter;
use App\DAO\Pagination\Pagination;
use App\DAO\ProcedimentoValorDAO;
use App\Entities\ProcedimentoValor\ProcedimentoValor;
use App\Mappers\ProcedimentoValorMapper;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;

class ProcedimentoValorService extends Service {

	public function registerProcedimentoValor(array $data_request, $idusr): ServiceResponse {

		$procedimento_registration_vm = ProcedimentoValorMapper::getProcedimentoValorRegistrationVMFromArray( $data_request );
		$procedimento_dao = new ProcedimentoValorDAO();
		$dao_response = $procedimento_dao->persistProcedimentoValorRegistrationVM( $procedimento_registration_vm, $idusr );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$data = [ 
					'id_convenio_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 getProcedimentoValorList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection): ServiceResponse {

		$procedimento_dao = new ProcedimentoValorDAO();
		$dao_response = $procedimento_dao->getProcedimentoValorList( $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 = ProcedimentoValorMapper::getProcedimentoValorsDetailsArrayFromProcedimentoValorDetailsVMCollection( 
				$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 getConvenioProcedimentoNotValor(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection): ServiceResponse {

		$procedimento_dao = new ProcedimentoValorDAO();
		$dao_response = $procedimento_dao->getConvenioProcedimentoNotValor( $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 = ProcedimentoValorMapper::getProcedimentoValorsDetailsArrayFromProcedimentoValorDetailsVMCollection( 
				$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 getProcedimentoValor(QueryFilter $query_filter): ServiceResponse {

		$procedimento_dao = new ProcedimentoValorDAO();
		$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 deleteProcedimentoValor(int $id_procedimento, int $id_unidade): ServiceResponse {

		$service_response = new ServiceResponse();
		$procedimento_dao = new ProcedimentoValorDAO();

		$dao_response = $procedimento_dao->gettotalRequisicaoProcedimentoValorUnidade( $id_procedimento, $id_unidade );
		$total = $dao_response->getData();

		if ($dao_response->getStatus() != DAOResponseInterface::SUCESS) {
			$this->setServiceResponse( $dao_response );
		} else {
			if ($total ['total'] == 0) {
				$dao_response = $procedimento_dao->deleteProcedimentoValor( $id_procedimento );
				$service_response = $this->setServiceResponse( $dao_response );
			} else {
				$dao_response = $procedimento_dao->inativaProcedimentoValor( $id_procedimento );
				$service_response = $this->setServiceResponse( $dao_response );
				// $service_response->setStatus( ServiceResponseInterface::HAVE_CONFLICTS );
			}
		}

		return $service_response;

	}

	public function ativarProcedimentoValor(int $id_procedimento): ServiceResponse {

		$service_response = new ServiceResponse();
		$procedimento_dao = new ProcedimentoValorDAO();

		$dao_response = $procedimento_dao->ativarProcedimentoValor( $id_procedimento );
		$service_response = $this->setServiceResponse( $dao_response );

		return $service_response;

	}

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

		$paciente_update_vm = ProcedimentoValorMapper::getProcedimentoValorUpdateVMFromArray( $data );
		$paciente_dao = new ProcedimentoValorDAO();
		$dao_response = $paciente_dao->persistProcedimentoValorUpdateVM( $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 getProcedimentosValorByUnidadeID(int $id_unidade): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ProcedimentoValorDAO();
		$daoResponse = $dao->getProcedimentosValorByUnidadeID( $id_unidade );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	public function getDadosProcedimentoValorByID(int $id_procedimento_valor): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ProcedimentoValorDAO();
		$daoResponse = $dao->getDadosProcedimentoValorByID( $id_procedimento_valor );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

}