<?php
/**
 * NOTICE OF LICENSE
 *
 * This file is licenced under the Software License Agreement.
 * With the purchase or the installation of the software in your application
 * you accept the licence agreement.
 *
 * You must not modify, adapt or create derivative works of this source code
 *
 * @author    GlobalPayments
 * @copyright Since 2021 GlobalPayments
 * @license   LICENSE
 */

/**
 * Global Payments Redirect Controller
 * Handles redirects from payment gateways
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

use GlobalPayments\PaymentGatewayProvider\Gateways\DiUiApms\Blik;
use GlobalPayments\PaymentGatewayProvider\Gateways\DiUiApms\BankSelect;
use GlobalPayments\PaymentGatewayProvider\Platform\Helper\CheckoutHelper;

class GlobalpaymentsRedirectModuleFrontController extends ModuleFrontController
{
    public function initContent()
    {
        parent::initContent();
        
        $action = Tools::getValue('action');
        $order_id = Tools::getValue('order_id');

        switch ($action) {
            case 'blik_redirect_handler':
                $this->handleBlikRedirect($order_id);

                // Log the incoming request for debugging
                PrestaShopLogger::addLog(
                    'BLIK Redirect: action=' . $action . ', order_id=' . $order_id, 1, null, 'GlobalPayments'
                );

                break;
            case 'ob_redirect_handler':
                $this->handleObRedirect($order_id);
                break;
            case 'hpp_return':
                if (Tools::getValue('isError') === '1') {
                    $this->errors[] = 'A payment error occurred. Please try again or try another payment method.';
                }

                $this->redirectWithNotifications(Tools::getValue('url'));
                break;
            default:
                PrestaShopLogger::addLog('BLIK Redirect: Unknown action: ' . $action, 3, null, 'GlobalPayments');
                // Use PrestaShop's built-in method instead of hardcoded URL
                $module = GlobalPayments::getModuleInstance();
                $checkoutHelper = new CheckoutHelper($module);
                $checkoutHelper->redirectToCartPage();
                break;
        }
    }

    private function handleBlikRedirect($order_id)
    {
        // Get module instance and create CheckoutHelper
        $module = GlobalPayments::getModuleInstance();
        $checkoutHelper = new CheckoutHelper($module);

        if (!$order_id) {
            PrestaShopLogger::addLog('BLIK Redirect: Missing order_id', 3, null, 'GlobalPayments');
            // Use PrestaShop's built-in method instead of hardcoded URL
            $checkoutHelper->redirectToCartPage();
            return;
        }

        try {
            // Call the static method in Blik class
            Blik::handleBlikRedirect();
        } catch (Exception $e) {
            PrestaShopLogger::addLog('BLIK Redirect Error: ' . $e->getMessage(), 3, null, 'GlobalPayments');
            // Use PrestaShop's built-in method instead of hardcoded URL
            $checkoutHelper->redirectToCartPage();
        }
    }

    private function handleObRedirect($order_id)
    {
        // Get module instance and create CheckoutHelper
        $module = GlobalPayments::getModuleInstance();
        $checkoutHelper = new CheckoutHelper($module);

        if (!$order_id) {
            PrestaShopLogger::addLog('Open Banking Redirect: Missing order_id', 3, null, 'GlobalPayments');
            // Use PrestaShop's built-in method instead of hardcoded URL
            $checkoutHelper->redirectToCartPage();
            return;
        }

        try {
            // Call the static method in BankSelect class
            BankSelect::handleObRedirect();
        } catch (Exception $e) {
            PrestaShopLogger::addLog('Open Banking Redirect Error: ' . $e->getMessage(), 3, null, 'GlobalPayments');
            // Use PrestaShop's built-in method instead of hardcoded URL
            $checkoutHelper->redirectToCartPage();
        }
    }
}
