<?php

namespace Oney\ThreeByFour\Controller\Payment;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Pricing\Helper\Data;
use Oney\ThreeByFour\Api\Marketing\SimulationInterface;
use Oney\ThreeByFour\Api\Marketing\BusinessTransactionsInterface;
use Oney\ThreeByFour\Helper\Config;

/**
 * Simulation Controller
 * 
 * Handles payment simulation requests for Oney ThreeByFour module.
 * Processes simulation requests and returns payment options based on amount and payment code.
 * 
 * @package Oney\ThreeByFour\Controller\Payment
 * @version 1.0.0
 * @author Oney
 * @since 1.0.0
 */
class Simulation extends \Magento\Framework\App\Action\Action
{
    /**
     * Simulation service instance
     * 
     * @var SimulationInterface
     * @version 1.0.0
     */
    protected $_simulationService;
    
    /**
     * Configuration helper instance
     * 
     * @var Config
     * @version 1.0.0
     */
    protected $helperConfig;

    /**
     * Pricing helper instance
     * 
     * @var Data
     * @version 1.0.0
     */
    protected $pricingHelper;

    /**
     * JSON result factory
     * 
     * @var JsonFactory
     * @version 1.0.1
     */
    protected $resultJsonFactory;

    /**
     * Constructor
     * 
     * @param Context $context Action context
     * @param SimulationInterface $simulation Simulation service
     * @param Data $pricingHelper Pricing helper
     * @param Config $helperConfig Configuration helper
     * @param JsonFactory $resultJsonFactory JSON result factory
     * @version 1.0.1
     */
    public function __construct(
        Context $context,
        SimulationInterface $simulation,
        Data $pricingHelper,
        Config $helperConfig,
        JsonFactory $resultJsonFactory
    ) {
        $this->_simulationService = $simulation;
        $this->pricingHelper = $pricingHelper;
        $this->helperConfig = $helperConfig;
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }

    /**
     * Execute action
     * 
     * Processes simulation request and returns payment options.
     * Returns specific simulation for payment code or all available options.
     * 
     * @return \Magento\Framework\Controller\Result\Json
     * @version 1.0.1
     */
    public function execute()
    {
        $resultJson = $this->resultJsonFactory->create();
        
        $amount = $this->currency((float)$this->getRequest()->getParam('amount'));
        
        if (($code = $this->getRequest()->getParam('code')) &&
            $this->helperConfig->isPaymentActiveForCode($this->getRequest()->getParam('code'))) {
            
            $data = $this->_simulationService->build($amount)->getSimulation([
                "payment_amount" => (float)round($amount, 2),
                "business_transaction_code" => str_replace('threebyfour_', '', $code)
            ]);
            
            return $resultJson->setData($data);
        }
        
        $data = $this->_simulationService->build($amount)->getSimulations();
        return $resultJson->setData($data);
    }

    /**
     * Format currency value
     * 
     * @param float $value Value to format
     * @return mixed Formatted currency value
     * @version 1.0.0
     */
    public function currency(float $value)
    {
        return $this->pricingHelper->currency($value, false, false);
    }
}
