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

namespace Awesomemotive\Rewardswp\Settings\Options\RewardsWidget;

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

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

use function Awesomemotive\Rewardswp\Helpers\has_rewardswp_plan_level;

/**
 * Class BrandForegroundModeOption.
 *
 * Controls whether brand foreground color is auto-detected or manually overridden.
 */
class BrandForegroundModeOption extends AbstractOption implements OptionInterface {
	/**
	 * Get the setting key used in the option array.
	 *
	 * @since 1.1.1
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'brand_foreground_mode';
	}

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

	/**
	 * Get the default value for the setting.
	 *
	 * @since 1.1.1
	 *
	 * @return string
	 */
	public function get_default_value(): string {
		return 'auto';
	}

	/**
	 * Get the value of the setting.
	 *
	 * Forces 'auto' for Lite users who don't have access to this feature,
	 * regardless of what's stored in the database.
	 *
	 * @since 1.1.1
	 *
	 * @return string
	 */
	public function get_value(): string {
		// Lite users always get 'auto' - preserve DB value for when they upgrade.
		if ( ! has_rewardswp_plan_level( 'basic' ) ) {
			return 'auto';
		}

		return parent::get_value();
	}

	/**
	 * Get the label for the setting.
	 *
	 * @since 1.1.1
	 *
	 * @return string
	 */
	public function get_label(): string {
		return esc_html__( 'Brand Foreground Mode', 'rewardswp' );
	}

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

	/**
	 * Get the available options.
	 *
	 * @since 1.1.1
	 *
	 * @return array<string, string>
	 */
	public function get_options(): array {
		return [
			'auto'  => esc_html__( 'Auto', 'rewardswp' ),
			'light' => esc_html__( 'Light', 'rewardswp' ),
			'dark'  => esc_html__( 'Dark', 'rewardswp' ),
		];
	}

	/**
	 * Sanitize the value.
	 *
	 * @since 1.1.1
	 *
	 * @param mixed $value The value to be sanitized.
	 *
	 * @return string
	 */
	public function sanitize( $value ): string {
		$valid_options = array_keys( $this->get_options() );
		return in_array( $value, $valid_options, true ) ? $value : $this->get_default_value();
	}
}
