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

namespace SimpleCaptcha;

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['anti_bot_honeypot']       = false;
		$settings['anti_bot_timing']         = false;
		$settings['ajax_comment_reset']      = false;
		$settings['login_canonical_only']    = false;
		$settings['register_canonical_only'] = false;
		$settings['woo_checkout_guest_only'] = 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['anti_bot_honeypot']       = ! empty( $existing['anti_bot_honeypot'] );
		$settings['anti_bot_timing']         = ! empty( $existing['anti_bot_timing'] );
		$settings['min_submit_seconds']      = max( 1, min( 30, (int) ( $existing['min_submit_seconds'] ?? 2 ) ) );
		$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['skip_rules']              = isset( $existing['skip_rules'] ) && is_array( $existing['skip_rules'] )
			? $existing['skip_rules']
			: [
				'enabled'     => false,
				'roles'       => [],
				'ips'         => [],
				'user_agents' => [],
			];

		return $settings;
	}
}
