<?php

namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\ViewModels\Extorno\ExtornoDetailsViewModel\ExtornoDetailsViewModel;
use App\DAO\ExtornoDAO;
use App\Mappers\ExtornoMapper;
use App\ViewModels\Extorno\ExtornoRegistrationViewModel\ExtornoRegistrationViewModel;
use App\ViewModels\CreditoCliente\CreditoClienteRegistrationViewModel\CreditoClienteRegistrationViewModel;
use App\Services\JWT\UsuarioLogado;

class ExtornoService extends Service {

	public function registerExtorno($data_request): ServiceResponse {

		$extorno_registration_vm = ExtornoMapper::getExtornoRegistrationVMFromRequest( $data_request );
		$extorno_dao = new ExtornoDAO();
		$dao_response = $extorno_dao->persistExtornoRegistrationVM( $extorno_registration_vm, $data_request );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				if ($extorno_registration_vm->getIdTipoExtorno() == 1) {
					$comanda_service = new ComandaService();
					$comanda_service_response = $comanda_service->getClienteIdByComandaId( $dao_response->getData()
						->getIdComanda() );
					$comanda_service_response_data = $comanda_service_response->getData();
					$clienteid = intval( $comanda_service_response_data [0] ['id_cliente'] );
					$clientecredito_reponse = $this->registerCreditoCliente( $extorno_registration_vm, $dao_response->getData()
						->getId(), $clienteid );
					if ($clientecredito_reponse->getStatus() == ServiceResponseInterface::SUCESS) {
						$data = [ 
								'id_extorno' => $dao_response->getData()
									->getId(),
								'credito' => $clientecredito_reponse->getData()
						];

						$service_response->setData( $data );
					} else {
						$service_response->setErrors( $clientecredito_reponse->getErrors() );
					}
					$service_response->setStatus( $clientecredito_reponse->getStatus() );
				}

                if ($extorno_registration_vm->getIdTipoExtorno() >= 2) {
                    $data = [
                        'id_extorno' => $dao_response->getData()
                            ->getId()
                    ];

                    $service_response->setData( $data );
                    $service_response->setStatus( ServiceResponseInterface::SUCESS );
                }
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				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 getExtornoByComandaIdList(int $comanda_id): ServiceResponse {

		$extorno_dao = new ExtornoDAO();
		$dao_response = $extorno_dao->getExtornosByComanda( $comanda_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();
			$extornos_details_array = ExtornoMapper::getExtornoDetailsArrayFromExtornoDetailsVMCollection( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
					'estornos' => $extornos_details_array,
					'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}

	private function registerCreditoCliente(ExtornoRegistrationViewModel $extorno_registration_vm, int $extornoId, int $clienteId): ServiceResponse {

		$service_response = new ServiceResponse();
		$creditoCliente_service = new CreditoClienteService();

		$usuarioid = UsuarioLogado::getUsuarioId();

		$creditoCliente_vm = CreditoClienteRegistrationViewModel::builder()->setExtornoId( $extornoId )
			->setClienteId( $clienteId )
			->setValor( $extorno_registration_vm->getValor() )
			->setSaldo( $extorno_registration_vm->getValor() )
			->setCriadoPor( $usuarioid )
			->setDataCriacao( $extorno_registration_vm->getDataCriacao() )
			->setHoraCriacao( $extorno_registration_vm->getHoraCriacao() )
			->build();

		$service_response = $creditoCliente_service->registerFromExtorno( $creditoCliente_vm );

		return $service_response;

	}

}

