<?php
/**
 * File: ReverseAndRefundRequest.php
 * Year: 2023
 */

namespace Tami\WooCommerce;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if(!class_exists('\Tami\WooCommerce\ReverseAndRefundRequest')) {
	class ReverseAndRefundRequest {
		public string $orderId;
		public ?float $amount = null;
		public ?string $reason = null;
		public ?string $securityHash = null;

		public function setOrderId( string $orderId ): ReverseAndRefundRequest {
			$this->orderId = $orderId;

			return $this;
		}

		public function setAmount( $amount ): ReverseAndRefundRequest {
			$this->amount = $amount;

			return $this;
		}

		public function setReason( ?string $reason ): ReverseAndRefundRequest {
			$this->reason = $reason;

			return $this;
		}

		public function setSecurityHash( string $securityHash ): ReverseAndRefundRequest {
			$this->securityHash = $securityHash;

			return $this;
		}

		public function generateHash(
			$merchantId,
			$terminalId,
			$secretKey,
			$fixedKidValue,
			$fixedKValue
		): bool {
			$requestBody = [
				'amount' => $this->amount,
				'orderId' => $this->orderId,
			];


			if(!class_exists('Tami\WooCommerce\HashGenerator'))
				include TAMI_PLUGIN_PATH . '/lib/HashGenerator.php';

			$HashGenerator = new HashGenerator($merchantId,$terminalId,$secretKey, $fixedKidValue, $fixedKValue);
			$this->setSecurityHash($HashGenerator->generateJWKSignature($requestBody));
			return true;
		}

		public function get(): object {
			return (object) [
				'orderId' => $this->orderId,
				'amount'  => number_format($this->amount, 2, '.', ''),
				'reason'  => $this->reason,
				'securityHash' => $this->securityHash
			];
		}
	}
}
