<?php
/**
 * AbstractRewardType.php.
 *
 * @package RewardsWP
 */

namespace Awesomemotive\Rewardswp\Abstracts;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Awesomemotive\Rewardswp\Interfaces\RewardTypeInterface;
use Awesomemotive\Rewardswp\Models\Reward;
use Awesomemotive\Rewardswp\Settings\Options\Referrals\RewardsOption;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;

/**
 * Abstract reward type class.
 *
 * @since 1.0.0
 */
abstract class AbstractRewardType implements RewardTypeInterface {

	/**
	 * Attached reward instance.
	 *
	 * @since 1.0.0
	 *
	 * @var Reward|null
	 */
	protected ?Reward $reward = null;

	/**
	 * Custom currency symbol for formatting.
	 *
	 * When null, uses the site's default currency symbol.
	 *
	 * @since 1.2.1
	 *
	 * @var string|null
	 */
	protected ?string $currency_symbol = null;

	/**
	 * Set a reward object, so it can be used internally by the reward type classes.
	 *
	 * This is useful if we need to interact with the reward to get information like dates and amounts.
	 *
	 * @since 1.0.0
	 *
	 * @param Reward $reward Reward instance to attach.
	 * @return void
	 */
	public function set_reward( Reward $reward ) {
		$this->reward = $reward;
	}

	/**
	 * Set currency symbol for formatting.
	 *
	 * @since 1.2.1
	 *
	 * @param string $symbol Currency symbol to use.
	 * @return self
	 */
	public function set_currency_symbol( string $symbol ): self {
		$this->currency_symbol = $symbol;
		return $this;
	}

	/**
	 * Get the currency symbol for formatting.
	 *
	 * @since 1.2.1
	 *
	 * @return string
	 */
	protected function get_currency_symbol(): string {
		return $this->currency_symbol ?? \Awesomemotive\Rewardswp\Helpers\get_site_currency_symbol();
	}

	/**
	 * Format a monetary reward amount with the currency symbol in the correct position.
	 *
	 * Honors a per-instance symbol override (see set_currency_symbol()) and resolves symbol
	 * placement from the store's currency settings, so euro stores render "5 €" rather than
	 * "€5". Use this instead of concatenating the symbol and amount by hand.
	 *
	 * @since 1.4.4
	 *
	 * @param float $amount The reward amount.
	 *
	 * @return string The formatted amount (e.g. "$5", "5 €").
	 */
	protected function format_reward_amount( float $amount ): string {
		$symbol           = $this->get_currency_symbol();
		$formatted_amount = \Awesomemotive\Rewardswp\Helpers\format_currency( $amount, '', true, false );

		return \Awesomemotive\Rewardswp\Helpers\apply_currency_symbol( $symbol, $formatted_amount );
	}
}
