<?php

namespace Sibs\Stargate\Api;

use Sibs\Stargate\Constants\OrderStatus;
use Sibs\Stargate\Constants\PaymentStatus;
use Sibs\Stargate\Constants\PaymentTypes;
use Sibs\Stargate\Exceptions\SibsException;
use Sibs\Stargate\Loggers\SibsLogger;
use Sibs\Stargate\Models\SibsGeneralModels;
use Sibs\Stargate\Utils\SibsVersion;

defined('ABSPATH') || exit;

class CancelAuthorization extends ApiAbstract
{
    /**
     * SIBS: Process.
     */
    public static function process(\WC_Order $wc_order)
    {
        SibsLogger::debug("[process_cancel_authorization] order_id: {$wc_order->get_id()}");

        $is_sibs_payment = SibsVersion::sibs_is_order_from_sibs($wc_order);

        if (!$is_sibs_payment) {
            throw new SibsException(
                __('Order is not a SIBS payment', 'sibs-woo-stg')
            );
        }

        $payment_status = $wc_order->get_status();
        $status_to_cancel_authorization = [
            OrderStatus::AUTHORIZED,
        ];

        if (!in_array($payment_status, $status_to_cancel_authorization)) {
            throw new SibsException(
                __('Order is not on a status to cancel authorization', 'sibs-woo-stg')
            );
        }

        $sibs_payment = SibsGeneralModels::get_transaction($wc_order);
        $payment_type = PaymentTypes::CAUT;
        $error_message = __('Cancel Authorization request has an ERROR', 'sibs-woo-stg');
        $amount = $wc_order->get_total();

        $backoffice_config = self::get_backoffice_config(
            $sibs_payment,
            $payment_type,
            $amount,
            $error_message
        );

        $backoffice_result = self::sibs_do_back_office_payment($wc_order, $backoffice_config);

        if (!$backoffice_result['is_valid']) {
            throw new SibsException($error_message);
        }

        if (PaymentStatus::SUCCESS !== $backoffice_result['response']['paymentStatus']) {
            throw new SibsException(
                __('Cancel Authorization request denied by SIBS', 'sibs-woo-stg')
            );
        }

        $cancel_auth_status = $backoffice_config['order_status'];
        SibsLogger::debug('[cancel_authorization] SUCCESS');

        $note_message = __('Order was Canceled', 'sibs-woo-stg');

        try {
            $wc_order->update_status($cancel_auth_status);
        } catch (\Throwable $t) {
            SibsLogger::critical($t->getMessage());
        }

        SibsGeneralModels::update_transaction($wc_order, [
            'payment_status' => $cancel_auth_status,
        ]);

        // Add authorization cancel note
        $wc_order->add_order_note($note_message);

        // Authorization Canceled with Success
        return sprintf(
            // translators: Order ID
            __('Authorization Canceled with success for order #%s', 'sibs-woo-stg'),
            $wc_order->get_id()
        );
    }

    /**
     * SIBS Cancel Authorization Order Add Button.
     *
     * @param mixed $order
     */
    public static function add_button($order)
    {
        $order_id = esc_attr($order->get_id());
        self::print_button($order_id, '4', esc_attr(__('SIBS Cancel Authorization', 'sibs-woo-stg')));
    }
}
