<?php
/**
 * The Amount model.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways\Shift4_APM\Responses\Models\Payment;

use JsonSerializable;

defined( 'ABSPATH' ) || exit;

/**
 * Class Amount_Model
 */
class Amount_Model implements JsonSerializable {

	/**
	 * The amount.
	 * Two exponents without a decimal are implied.
	 *
	 * @var int $amount
	 */
	private int $amount;

	/**
	 * The currency.
	 *
	 * @var string $currency
	 */
	public string $currency;

	/**
	 * Amount_Model constructor.
	 *
	 * @param array $amount_data The amount data.
	 */
	public function __construct( array $amount_data ) {
		$this->amount   = $amount_data['amount'];
		$this->currency = $amount_data['currency'];
	}

	/**
	 * Get amount.
	 *
	 * TODO: Update if support is added for currencies without decimals.
	 *
	 * @return float
	 */
	public function get_amount(): float {
		return $this->amount / 100;
	}

	/**
	 * Get amount in display format.
	 *
	 * @return string
	 */
	public function get_display_amount(): string {
		// Format with wc_price() to get the correct currency symbol.
		return wc_price( $this->get_amount(), array( 'currency' => $this->currency ) );
	}

	/**
	 * {@inheritDoc}
	 */
	public function jsonSerialize(): array {
		return array(
			'amount'   => $this->amount,
			'currency' => $this->currency,
		);
	}
}
