<?php
/**
 * Premium feature gating for Cloudflare Turnstile.
 *
 * @package CloudflareTurnstile
 */

namespace CloudflareTurnstile;

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

class FeatureGate {

	/**
	 * Settings whose implementation lives in the Pro overlay (not in Lite).
	 *
	 * @var list<string>
	 */
	public const OVERLAY_SETTING_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',
		'disable_submit',
		'ajax_comment_reset',
		'login_canonical_only',
		'register_canonical_only',
		'woo_checkout_guest_only',
		'perf_compat',
		'resource_hints',
		'blocksy_compat',
	];

	public static function isOverlaySetting( string $key ): bool {
		return $key === 'skip_rules' || $key === 'failsafe_mode' || str_starts_with( $key, 'failsafe_' ) || in_array( $key, self::OVERLAY_SETTING_KEYS, true );
	}

	public static function isPremiumLicensed(): bool {
		return function_exists( 'wp_powersuite_freemium_unlocked' ) && wp_powersuite_freemium_unlocked( 'cloudflare-turnstile' );
	}

	/**
	 * Disable Pro-overlay settings at runtime when the 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::OVERLAY_SETTING_KEYS as $key ) {
			$settings[ $key ] = false;
		}
		$settings['failsafe_mode']                 = 'off';
		$settings['failsafe_recaptcha_site_key']   = '';
		$settings['failsafe_recaptcha_secret_key'] = '';
		$settings['skip_rules']                    = [
			'enabled'     => false,
			'roles'       => [],
			'ips'         => [],
			'user_agents' => [],
		];

		return $settings;
	}

	/**
	 * Ignore submitted overlay fields when the 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::OVERLAY_SETTING_KEYS as $key ) {
			$settings[ $key ] = ! empty( $existing[ $key ] );
		}
		$settings['failsafe_mode'] = isset( $existing['failsafe_mode'] ) ? sanitize_key( (string) $existing['failsafe_mode'] ) : 'off';
		if ( ! in_array( $settings['failsafe_mode'], [ 'off', 'allow', 'recaptcha' ], true ) ) {
			$settings['failsafe_mode'] = 'off';
		}
		$settings['failsafe_recaptcha_site_key'] = isset( $existing['failsafe_recaptcha_site_key'] )
			? sanitize_text_field( (string) $existing['failsafe_recaptcha_site_key'] )
			: '';
		if ( array_key_exists( 'failsafe_recaptcha_secret_key', $existing ) ) {
			$settings['failsafe_recaptcha_secret_key'] = $existing['failsafe_recaptcha_secret_key'];
		}
		$settings['skip_rules'] = isset( $existing['skip_rules'] ) && is_array( $existing['skip_rules'] )
			? $existing['skip_rules']
			: [
				'enabled'     => false,
				'roles'       => [],
				'ips'         => [],
				'user_agents' => [],
			];

		return $settings;
	}
}
