<?php

namespace Oney\ThreeByFour\Gateway\Http;

use Magento\Payment\Gateway\Http\TransferBuilder;
use Magento\Payment\Gateway\Http\TransferFactoryInterface;
use Oney\ThreeByFour\Helper\Config;
use Oney\ThreeByFour\Logger\Logger;
use Laminas\Http\Request;

/**
 * Transfer Factory
 * 
 * Creates HTTP transfer objects for Oney payment gateway requests.
 * Handles request preparation, headers, and API endpoint configuration.
 * 
 * @package Oney\ThreeByFour\Gateway\Http
 * @version 1.0.0
 * @author Oney
 * @since 1.0.0
 */
class TransferFactory implements TransferFactoryInterface
{
    /**
     * Payment step constant
     * 
     * @const string
     * @version 1.0.0
     */
    const PAYMENT_STEP = "purchase";
    
    /**
     * Payment API constant
     * 
     * @const string
     * @version 1.0.0
     */
    const PAYMENT_API = "api_payment";
    
    /**
     * Country API constant
     * 
     * @const string
     * @version 1.0.0
     */
    const COUNTRY_API = "country";

    /**
     * Transfer builder instance
     * 
     * @var TransferBuilder
     * @version 1.0.0
     */
    protected $transferBuilder;

    /**
     * Configuration helper instance
     * 
     * @var Config
     * @version 1.0.0
     */
    protected $_helperConfig;

    /**
     * Logger instance
     * 
     * @var Logger
     * @version 1.0.0
     */
    protected $_logger;

    /**
     * Constructor
     * 
     * @param TransferBuilder $transferBuilder Transfer builder
     * @param Config $config Configuration helper
     * @param Logger $logger Logger instance
     * @version 1.0.0
     */
    public function __construct(
        TransferBuilder $transferBuilder,
        Config $config,
        Logger $logger
    ) {
        $this->transferBuilder = $transferBuilder;
        $this->_logger = $logger;
        $this->_helperConfig = $config;
    }

    /**
     * Create HTTP transfer object
     * 
     * @param array $request Request data
     * @return \Magento\Payment\Gateway\Http\TransferInterface
     * @version 1.0.0
     */
    public function create(array $request)
    {
        $this->_logger->info("[SUCCESS] [JSON] Payment API 1.2 - Preparing transfer: ", [
            'headers' => $this->prepareHeaders(),
            'uri' => $this->_helperConfig->getUrlForStep(self::PAYMENT_STEP),
            'method' => Request::METHOD_POST
        ]);
        
        return $this->transferBuilder
            ->setBody($request)
            ->setHeaders($this->prepareHeaders())
            ->setUri($this->_helperConfig->getUrlForStep(self::PAYMENT_STEP))
            ->setMethod(Request::METHOD_POST)
            ->build();
    }

    /**
     * Prepare HTTP headers for API request
     * 
     * @return array Headers array
     * @version 1.0.0
     */
    protected function prepareHeaders(): array
    {
        return [
            "X-Oney-Authorization" => $this->_helperConfig->getApiConfigValue(self::PAYMENT_API),
            "X-Oney-Partner-Country-Code" => $this->_helperConfig->getCountrySpecificationsConfigValue(self::COUNTRY_API),
            "X-Oney-Secret" => $this->_helperConfig->getSecret()
        ];
    }
}
