<?php

namespace WeeConnectPay\Api\Requests;

use WC_Order;
use WeeConnectPay\Dependencies\Psr\Http\Message\ResponseInterface;
use WeeConnectPay\Api\ApiClient;
use WeeConnectPay\Api\ApiEndpoints;

/**
 * Capture a pre-authorized charge
 *
 * @package WeeConnectPay
 * @subpackage WeeConnectPay/Api/Requests
 * @since 3.17.0
 */
class CaptureCloverChargeRequest extends ApiClient {

	/**
	 * POST request to capture a pre-authorized charge
	 *
	 * @param string $cloverChargeUuid The UUID of the pre-authorized charge
	 * @param ?int $amount The amount to capture in cents
	 * @param string $ipAddress The customer's IP address
	 * @return ResponseInterface
	 * @since 3.17.0
	 */
	public function POST( string $cloverChargeUuid, ?int $amount, string $ipAddress ): ResponseInterface {
		return $this->client->post(
			ApiEndpoints::captureCharge($cloverChargeUuid),
			self::setOptions($amount, $ipAddress)
		);
	}

	/**
	 * Set request options
	 *
	 * @param ?int $amount The amount to capture in cents
	 * @param string $ipAddress The customer's IP address
	 * @return array
	 * @since 3.17.0
	 */
	private static function setOptions( ?int $amount, string $ipAddress ): array {
		return [
			'form_params' => self::setRequestBody($amount, $ipAddress),
		];
	}

	/**
	 * Set the request body
	 *
	 * @param ?int $amount The amount to capture in cents
	 * @param string $ipAddress The customer's IP address
	 * @return array
	 * @since 3.17.0
	 */
	private static function setRequestBody( ?int $amount, string $ipAddress ): array {
		$params = array(
			'ip_address'          => $ipAddress,
			'integration_version' => WEECONNECT_VERSION,
		);

		if ( $amount ) {
			$params['amount'] = $amount;
		}

		return $params;
	}
}
