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

namespace Awesomemotive\Rewardswp\Settings\Options\Emails;

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

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

/**
 * Class EmailLogoOption.
 */
class EmailLogoOption extends AbstractOption implements OptionInterface {
	/**
	 * Default size for email logo previews.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	const EMAIL_LOGO_DEFAULT_SIZE = [ 360, 120 ];
	/**
	 * Get the setting key used in the option array.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'email_logo';
	}

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

	/**
	 * Get the default value for the setting.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_default_value(): array {
		return [
			'attachment_id' => 0,
			'size'          => 'medium',
		];
	}

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

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

	/**
	 * Sanitize the email logo array.
	 *
	 * @since 1.0.0
	 *
	 * @param mixed $value The value to be sanitized.
	 *
	 * @return array
	 */
	public function sanitize( $value ): array {
		$defaults = $this->get_default_value();

		// If value is not an array, return defaults.
		if ( ! is_array( $value ) ) {
			return $defaults;
		}

		$allowed_sizes = [ 'thumbnail', 'medium', 'large', 'full' ];

		return [
			'attachment_id' => absint( $value['attachment_id'] ?? $defaults['attachment_id'] ),
			'size'          => in_array( $value['size'] ?? '', $allowed_sizes, true )
				? $value['size']
				: $defaults['size'],
		];
	}
}
