<?php

namespace Sipago\WooCommerce\SipagoCheckout\Controllers;

use DateTime;
use DateTimeZone;
use Sipago\WooCommerce\SipagoCheckout\Helpers\SessionMessageHelper;
use Sipago\WooCommerce\SipagoCheckout\Models\Transaction;
use WC_Gateway_Sipago_Sipago_REST;
use WC_Order;

class ThankYouPageController
{



    public function show($orderId)
    {
        $woocommerceOrder = new WC_Order($orderId);
        $payment_method= $woocommerceOrder->get_payment_method();
        if($payment_method!="sipago_pay"){
            return;
        }
        $sipagopayPlusPaymentGateway = new WC_Gateway_Sipago_Sipago_REST();



        $sipagopayTransaction = Transaction::getApprovedByOrderId($orderId);
        if ($sipagopayTransaction === null) {
            // TODO: Revisar porqué no se muestra el mensaje de abajo.
            // if (SessionMessageHelper::exists()){
            //     SessionMessageHelper::printMessage();
            //     return;
            // }
            wc_print_notice('Transacción <strong>fallida</strong>.', 'error');

            require __DIR__.'/../../views/error-order.php';
            return;
        }

        if ($sipagopayTransaction->status !== Transaction::STATUS_APPROVED) {
            return wp_redirect($woocommerceOrder->get_cancel_order_url());
        }

        // Transacción aprobada
        wc_print_notice(__('Transacción aprobada', 'sipago_wc_plugin'), 'success');
        $finalResponse = json_decode($sipagopayTransaction->sipago_response, true);

        $data = (new ResponseSipagoController([]))->getTransactionDetails($finalResponse);
        list($orderNumber,$authorizationCode,$referenceNumber, $amount, $transactionResponse,$date_accepted) = $data;
        require __DIR__.'/../../views/order-summary.php';

        
        
    }
}
