<?php

namespace WeeConnectPay\Api\Requests;

use WC_Order;
use WeeConnectPay\Dependencies\GuzzleHttp\Exception\GuzzleException;
use WeeConnectPay\Dependencies\Psr\Http\Message\ResponseInterface;
use WeeConnectPay\Api\ApiClient;
use WeeConnectPay\Api\ApiEndpoints;
use WeeConnectPay\WordPress\Plugin\includes\WeeConnectPayUtilities;
use WeeConnectPay\WordPress\Plugin\includes\WeeConnectPayHelper;

class CreateCloverOrderChargeRequest extends ApiClient {

	/**
	 * POST request
	 *
	 * @param bool $threeDSecureOptIn  When true, attach a 3-D Secure opt-in marker + browser_info to the request body.
	 * @param array|null $browserInfo  Output of `window._3DSUtil.getBrowserInfo()` from the client, or null. browser_ip is filled in server-side.
	 */
	public function POST(string $cloverOrderUuid, string $tokenizedCard, string $ipAddress, ?int $amount, WC_Order $order, bool $threeDSecureOptIn = false, ?array $browserInfo = null): ResponseInterface {
        return $this->client->post( ApiEndpoints::createOrderCharge($cloverOrderUuid), self::setOptions($tokenizedCard, $ipAddress, $amount, $order, $threeDSecureOptIn, $browserInfo));
	}

    /**
     * @param string $tokenizedCard
     * @param string $ipAddress
     * @param int|null $amount
     * @return array
     * @updated 3.4.0
     */
	private static function setOptions(string $tokenizedCard, string $ipAddress, ?int $amount, WC_Order $order, bool $threeDSecureOptIn = false, ?array $browserInfo = null ): array {
		$options['form_params'] = self::setRequestBody( $tokenizedCard, $ipAddress , $amount, $order, $threeDSecureOptIn, $browserInfo);

		return $options;
	}

    /**
     * Set the request body
     *
     * @param string $tokenizedCard
     * @param string $ipAddress
     * @param int|null $amount
     * @return array
     */
	private static function setRequestBody(string $tokenizedCard, string $ipAddress, ?int $amount, WC_Order $order, bool $threeDSecureOptIn = false, ?array $browserInfo = null): array {
        $params = array(
			'ip_address'          => $ipAddress,
            'integration_version' => WEECONNECT_VERSION,
            'amount'              => $amount, // If not specified, will pay the total of the order -- IMPORTANT (THIS VARIABLE MISSING CREATES A LOT OF CLOVER-SIDE "EDGE CASES")
            'tokenized_card'      => $tokenizedCard,
        );

        if ( 'subscription' === WeeConnectPayHelper::get_order_type( $order ) ) {
            // subscription = initial order of a subscription.
            $params['save_credential'] = true; // allows the tokenized card to be saved to the backend.
        } elseif ( in_array( WeeConnectPayHelper::get_order_type( $order ), array( 'subscription_renewal', 'subscription_early_renewal' ), true ) ) {
            // subscription_renewal = renewal order of a subscription. subscription_early_renewal = manual renewal of a subscription by a customer.
            $params['saved_credential'] = WeeConnectPayHelper::get_token_uuid_from_subscription_order( $order ); // uses the saved tokenized card for the renewal payment.
            unset( $params['tokenized_card'] ); // not needed for renewal payments.
        }

        if ( $threeDSecureOptIn ) {
            $params['three_d_secure_opt_in'] = true;
            $params['browser_info']          = $browserInfo ?? array();
        }

        return $params;
	}
}
