<?php
/**
 * Premium feature gating for Google reCAPTCHA.
 *
 * @package GoogleRecaptcha
 */

namespace GoogleRecaptcha;

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

class FeatureGate {

	/** @var list<string> */
	private const INTEGRATION_KEYS = [
		'basic_forms',
		'cf7',
		'wpforms',
		'formidable',
		'fluent_forms',
		'elementor_forms',
		'integration_gravity',
		'integration_jetpack',
		'integration_bbpress',
		'integration_buddypress',
		'integration_ultimate_member',
		'integration_memberpress',
		'integration_edd',
		'integration_pmp',
		'integration_mc4wp',
	];

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

	/**
	 * Strip premium settings at runtime when license is inactive.
	 *
	 * @param array<string, mixed> $settings Normalized module settings.
	 * @return array<string, mixed>
	 */
	public static function applyPremiumTierLimits( array $settings ): array {
		if ( self::isPremiumLicensed() ) {
			return $settings;
		}

		foreach ( self::INTEGRATION_KEYS as $key ) {
			$settings[ $key ] = false;
		}

		$settings['widget_language']         = '';
		$settings['disable_submit']          = false;
		$settings['ajax_comment_reset']      = false;
		$settings['login_canonical_only']    = false;
		$settings['register_canonical_only'] = false;
		$settings['woo_checkout_guest_only'] = false;
		$settings['perf_compat']             = false;
		$settings['resource_hints']          = false;
		$settings['blocksy_compat']          = false;
		$settings['skip_rules']              = [
			'enabled'     => false,
			'roles'       => [],
			'ips'         => [],
			'user_agents' => [],
		];

		return $settings;
	}

	/**
	 * Ignore submitted premium fields when license is inactive (preserve stored values).
	 *
	 * @param array<string, mixed> $settings Sanitized 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;
		}

		foreach ( self::INTEGRATION_KEYS as $key ) {
			$settings[ $key ] = ! empty( $existing[ $key ] );
		}

		$settings['widget_language']         = isset( $existing['widget_language'] ) ? (string) $existing['widget_language'] : '';
		$settings['disable_submit']          = ! empty( $existing['disable_submit'] );
		$settings['ajax_comment_reset']      = ! empty( $existing['ajax_comment_reset'] );
		$settings['login_canonical_only']    = ! empty( $existing['login_canonical_only'] );
		$settings['register_canonical_only'] = ! empty( $existing['register_canonical_only'] );
		$settings['woo_checkout_guest_only'] = ! empty( $existing['woo_checkout_guest_only'] );
		$settings['perf_compat']             = ! empty( $existing['perf_compat'] );
		$settings['resource_hints']          = ! empty( $existing['resource_hints'] );
		$settings['blocksy_compat']          = ! empty( $existing['blocksy_compat'] );
		$settings['skip_rules']              = isset( $existing['skip_rules'] ) && is_array( $existing['skip_rules'] )
			? $existing['skip_rules']
			: [
				'enabled'     => false,
				'roles'       => [],
				'ips'         => [],
				'user_agents' => [],
			];

		return $settings;
	}
}
