<?php

declare(strict_types=1);

namespace GlobalPayments\Api\Services;

use GlobalPayments\Api\Entities\GpApi\GpApiSessionInfo;
use GlobalPayments\Api\Gateways\GpApiConnector;
use GlobalPayments\Api\ServiceConfigs\Gateways\GpApiConfig;
use GlobalPayments\Api\Entities\GpApi\AccessTokenInfo;

class GpApiService
{
    public static function generateTransactionKey(GpApiConfig $config): AccessTokenInfo
    {
        if (!isset($config->accessTokenProvider)) {
            $config->accessTokenProvider = new GpApiSessionInfo();
        }
        
        $gateway = new GpApiConnector($config);
        
        if (empty($gateway->serviceUrl)) {
            $gateway->serviceUrl = $config->resolveServiceUrl();
        }
        
        $gateway->requestLogger = $config->requestLogger;
        $gateway->webProxy = $config->webProxy;
        $gateway->dynamicHeaders = $config->dynamicHeaders;

        $data = $gateway->getAccessToken();

        $accessTokenInfo = new AccessTokenInfo();
        $accessTokenInfo->accessToken = $data->token;
        $accessTokenInfo->dataAccountName = $data->getDataAccountName();
        $accessTokenInfo->disputeManagementAccountName = $data->getDisputeManagementAccountName();
        $accessTokenInfo->transactionProcessingAccountName = $data->getTransactionProcessingAccountName();
        $accessTokenInfo->tokenizationAccountName = $data->getTokenizationAccountName();
        $accessTokenInfo->riskAssessmentAccountName = $data->getRiskAssessmentAccountName();
        $accessTokenInfo->merchantManagementAccountName = $data->getMerchantManagementAccountName();
        $accessTokenInfo->fileProcessingAccountName = $data->getFileProcessingAccountName();
        $accessTokenInfo->dataAccountID = $data->getDataAccountID();
        $accessTokenInfo->disputeManagementAccountID = $data->getDisputeManagementAccountID();
        $accessTokenInfo->transactionProcessingAccountID = $data->getTransactionProcessingAccountID();
        $accessTokenInfo->tokenizationAccountID = $data->getTokenizationAccountID();
        $accessTokenInfo->riskAssessmentAccountID = $data->getRiskAssessmentAccountID();
        $accessTokenInfo->merchantManagementAccountID = $data->getMerchantManagementAccountID();
        $accessTokenInfo->fileProcessingAccountID = $data->getFileProcessingAccountID();

        return $accessTokenInfo;
    }
}