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

namespace Awesomemotive\Rewardswp\Settings\Options\Referrals;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractOption;
use Awesomemotive\Rewardswp\Interfaces\OptionInterface;

/**
 * Class RewardExpirationOption.
 */
class RewardExpirationOption extends AbstractOption implements OptionInterface {
	/**
	 * Get the setting key used in the option array.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'reward_expiration';
	}

	/**
	 * Get the setting group.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_group(): string {
		return 'referrals';
	}

	/**
	 * Get the default value for the setting.
	 *
	 * @since 1.0.0
	 *
	 * @return int
	 */
	public function get_default_value(): int {
		return '+3 months';
	}

	/**
	 * Get the label for the setting.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_label(): string {
		return esc_html__( 'Reward Expiration', 'rewardswp' );
	}

	/**
	 * Get the type of the setting (text, textarea, select, etc).
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_type(): string {
		return 'radio';
	}

	/**
	 * Get the available options for the option.
	 *
	 * Must return an array with the value using the amount of time to add to the DateTime::modify method.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_options(): array {
		return [
			[
				'value' => '+7 days',
				'label' => esc_html__( '7 days', 'rewardswp' ),
			],
			[
				'value' => '+3 months',
				'label' => esc_html__( '3 months', 'rewardswp' ),
			],
			[
				'value' => '+6 months',
				'label' => esc_html__( '6 months', 'rewardswp' ),
			],
			[
				'value' => '+1 year',
				'label' => esc_html__( '1 year', 'rewardswp' ),
			],
		];
	}

	/**
	 * Sanitize field.
	 *
	 * @since 1.0.0
	 *
	 * @param mixed $value The value to be sanitized.
	 *
	 * @return string
	 */
	public function sanitize( $value ): string {
		$options = $this->get_options();

		$values = array_map(
			function ( $item ) {
				return $item['value'];
			},
			$options
		);

		return in_array( $value, $values, true )
			? sanitize_text_field( $value )
			: $this->get_default_value();
	}
}
