<?php
/**
 * Freemium feature gating for the Accessibility Widget.
 *
 * The module loads for everyone. Advanced visitor tools are forced off when the
 * site has no valid license. All admin display/branding customization stays free.
 *
 * @package AccessibilityWidget
 */

namespace AccessibilityWidget;

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

final class FeatureGate {

	/**
	 * Visitor tools that require a valid license. These are keys inside the
	 * `features` settings map.
	 *
	 * @var list<string>
	 */
	public const PREMIUM_FEATURE_KEYS = [
		'accessibility_profiles',
		'protanopia',
		'deuteranopia',
		'tritanopia',
		'low_saturation',
		'high_saturation',
		'dyslexia_font',
		'big_black_cursor',
		'text_to_speech',
		'link_navigator',
		'page_structure',
		'mute_media',
		'simplify_layout',
	];

	public static function isPremiumLicensed(): bool {
		return function_exists( 'wp_powersuite_is_license_valid' ) && wp_powersuite_is_license_valid();
	}

	/**
	 * License page when Pro is installed, otherwise the public purchase URL.
	 */
	public static function upsellUrl(): string {
		if ( function_exists( 'wp_powersuite_premium_upsell_url' ) ) {
			return wp_powersuite_premium_upsell_url();
		}

		return defined( 'WP_POWERSUITE_PRODUCT_URL' ) ? WP_POWERSUITE_PRODUCT_URL : 'https://wppowersuite.com/';
	}

	public static function isPremiumFeature( string $feature_key ): bool {
		return in_array( $feature_key, self::PREMIUM_FEATURE_KEYS, true );
	}

	/**
	 * Force premium feature flags off when the site is not licensed, so disabled
	 * tools never render or load on the frontend.
	 *
	 * @param array<string, mixed> $settings Normalized settings.
	 * @return array<string, mixed>
	 */
	public static function applyPremiumTierLimits( array $settings ): array {
		if ( self::isPremiumLicensed() ) {
			return $settings;
		}

		$features = isset( $settings['features'] ) && is_array( $settings['features'] ) ? $settings['features'] : [];
		foreach ( self::PREMIUM_FEATURE_KEYS as $key ) {
			$features[ $key ] = false;
		}
		$settings['features'] = $features;

		return $settings;
	}

	/**
	 * Preserve previously stored premium flags when an unlicensed site saves
	 * settings, so toggling free options never silently wipes premium choices.
	 *
	 * @param array<string, mixed> $settings Settings about to be saved.
	 * @param array<string, mixed> $existing Previously stored settings.
	 * @return array<string, mixed>
	 */
	public static function preservePremiumSettingsOnSave( array $settings, array $existing ): array {
		if ( self::isPremiumLicensed() ) {
			return $settings;
		}

		$new_features      = isset( $settings['features'] ) && is_array( $settings['features'] ) ? $settings['features'] : [];
		$existing_features = isset( $existing['features'] ) && is_array( $existing['features'] ) ? $existing['features'] : [];

		foreach ( self::PREMIUM_FEATURE_KEYS as $key ) {
			$new_features[ $key ] = ! empty( $existing_features[ $key ] );
		}

		$settings['features'] = $new_features;

		return $settings;
	}
}
