<?php
/**
 * Premium feature gating for Simple CAPTCHA.
 *
 * @package SimpleCaptcha
 */

namespace SimpleCaptcha;

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',
		'anti_bot_honeypot',
		'anti_bot_timing',
		'ajax_comment_reset',
		'login_canonical_only',
		'register_canonical_only',
		'woo_checkout_guest_only',
	];

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

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

	/**
	 * 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['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['min_submit_seconds'] = max( 1, min( 30, (int) ( $existing['min_submit_seconds'] ?? 2 ) ) );
		$settings['skip_rules']         = isset( $existing['skip_rules'] ) && is_array( $existing['skip_rules'] )
			? $existing['skip_rules']
			: [
				'enabled'     => false,
				'roles'       => [],
				'ips'         => [],
				'user_agents' => [],
			];

		return $settings;
	}
}

if ( function_exists( 'add_filter' ) ) {
	add_filter(
		'wp_powersuite_preserve_pro_settings_on_save',
		static function ( $settings, string $module_id, $existing ) {
			if ( $module_id !== 'simple-captcha' || ! is_array( $settings ) ) {
				return $settings;
			}

			return FeatureGate::preservePremiumSettingsOnSave( $settings, is_array( $existing ) ? $existing : [] );
		},
		10,
		3
	);
}
