<?php
/**
 * Receipt Discount model.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Abstracts\Hub_API\Models\Receipts;

use JsonSerializable;
use Northmill_Online_Shared\Helpers\General_Helpers;

/**
 * Class Receipt_Discount_Model
 */
class Receipt_Discount_Model implements JsonSerializable {
	/**
	 * Type of discount.
	 * Can be 'None', 'Percent', 'Amount'.
	 *
	 * @var string
	 */
	protected string $DiscountType;

	/**
	 * Amount of the discount. Sum for entire article row.
	 * (Not per unit)
	 *
	 * @var string
	 */
	protected string $Amount;

	/**
	 * Description of the discount.
	 *
	 * @var string
	 */
	protected string $Description;

	/**
	 * Percentage of the discount.
	 * Null if not a percentage discount.
	 *
	 * @var int|null
	 */
	protected ?int $Percentage;

	/**
	 * Construct the Receipt_Discount_Model.
	 *
	 * @param string       $discount_type Type of discount.
	 * @param float|string $amount        Discount amount.
	 * @param string       $description   Description of the discount.
	 * @param int|null     $percentage    Percentage of the discount. Only for some discount types.
	 */
	public function __construct( string $discount_type, float|string $amount, string $description, ?int $percentage = null ) {
		$this->DiscountType = $discount_type;
		$this->Amount       = General_Helpers::round_and_format_number( $amount );
		$this->Description  = $description;
		$this->Percentage   = $percentage;
	}

	/**
	 * Get the discount amount.
	 *
	 * @return string
	 */
	public function get_amount(): string {
		return $this->Amount;
	}

	/**
	 * Get the discount type.
	 *
	 * @return string
	 */
	public function get_type(): string {
		return $this->DiscountType;
	}

	/**
	 * {@inheritDoc}
	 */
	public function jsonSerialize(): array {
		return array(
			'DiscountType' => $this->DiscountType,
			'Amount'       => $this->Amount,
			'Description'  => $this->Description,
			'Percentage'   => $this->Percentage,
		);
	}
}
