<?php
/**
 * Pro/free feature gating for AI Forms.
 *
 * @package BasicForms
 */

namespace BasicForms;

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

class FeatureGate {

	/** @var list<string> */
	public const FREE_FIELD_TYPES = [ 'text', 'email', 'textarea', 'number' ];

	/** @var list<string> */
	public const PREMIUM_SETTINGS_KEYS = [
		'redirect_url',
		'enable_autoreply',
		'autoreply_subject',
		'autoreply_message',
	];

	/** @var list<string> */
	public const CAPTCHA_PROVIDERS = [ 'simple', 'google', 'turnstile' ];

	public static function isPro(): bool {
		$licensed = function_exists( 'wp_powersuite_is_license_valid' ) && wp_powersuite_is_license_valid();

		/** @param bool $licensed Whether WP PowerSuite license is valid. */
		return (bool) apply_filters( 'wpps_ai_forms_is_pro', $licensed );
	}

	public static function aiFormBuilderEnabled(): bool {
		if ( ! self::isPro() ) {
			return false;
		}

		return AiSettings::isEnabled();
	}

	public static function formDesignEnabled(): bool {
		return self::isPro();
	}

	public static function conditionalFieldsEnabled(): bool {
		return self::isPro();
	}

	public static function captchaProvidersEnabled(): bool {
		return self::isPro();
	}

	public static function isPremiumFieldType( string $type ): bool {
		$type = sanitize_key( $type );

		return $type !== '' && ! in_array( $type, self::FREE_FIELD_TYPES, true );
	}

	/**
	 * @return list<string>
	 */
	public static function getFreeFieldTypes(): array {
		return self::FREE_FIELD_TYPES;
	}

	/**
	 * @param array<int, array<string, mixed>> $fields
	 * @param array<int, array<string, mixed>> $existing
	 * @return array<int, array<string, mixed>>
	 */
	public static function applyFieldTierLimits( array $fields, array $existing = [] ): array {
		if ( self::isPro() ) {
			return $fields;
		}

		$output = [];
		foreach ( $fields as $field ) {
			if ( ! is_array( $field ) ) {
				continue;
			}

			if ( ! self::conditionalFieldsEnabled() ) {
				unset( $field['show_if'] );
			}

			$output[] = $field;
		}

		return array_values( $output );
	}

	/**
	 * @param array<string, mixed> $settings
	 * @param array<string, mixed> $existing
	 * @return array<string, mixed>
	 */
	public static function preservePremiumFormSettingsOnSave( array $settings, array $existing ): array {
		if ( self::isPro() ) {
			return $settings;
		}

		$existing = is_array( $existing ) ? $existing : [];

		foreach ( self::PREMIUM_SETTINGS_KEYS as $key ) {
			if ( array_key_exists( $key, $existing ) ) {
				$settings[ $key ] = $existing[ $key ];
			} elseif ( $key === 'enable_autoreply' ) {
				$settings[ $key ] = '';
			} else {
				$settings[ $key ] = '';
			}
		}

		$provider = (string) ( $settings['captcha_provider'] ?? '' );
		if ( in_array( $provider, self::CAPTCHA_PROVIDERS, true ) ) {
			$existing_provider = (string) ( $existing['captcha_provider'] ?? '' );
			$settings['captcha_provider'] = in_array( $existing_provider, self::CAPTCHA_PROVIDERS, true )
				? $existing_provider
				: '';
		}

		return $settings;
	}

	/**
	 * @param array<string, mixed> $design
	 * @param array<string, mixed> $existing
	 * @return array<string, string>
	 */
	public static function preserveFormDesignOnSave( array $design, array $existing ): array {
		if ( self::isPro() ) {
			return DesignDefaults::normalizeForAdminForm( $design );
		}

		$defaults = DesignDefaults::get();
		$existing = is_array( $existing ) ? $existing : [];

		return DesignDefaults::normalizeForAdminForm( wp_parse_args( $existing, $defaults ) );
	}

	/**
	 * @param array<string, mixed> $settings
	 * @param array<string, mixed> $existing
	 * @return array<string, mixed>
	 */
	public static function preservePremiumSettingsOnSave( array $settings, array $existing ): array {
		if ( self::isPro() ) {
			return $settings;
		}

		$defaults = AiSettings::defaults();
		$existing = array_merge( $defaults, is_array( $existing ) ? $existing : [] );

		if ( array_key_exists( 'ai_enabled', $existing ) ) {
			$settings['ai_enabled'] = $existing['ai_enabled'];
		}
		if ( isset( $existing['provider'] ) ) {
			$settings['provider'] = $existing['provider'];
		}
		if ( isset( $existing['model'] ) ) {
			$settings['model'] = $existing['model'];
		}

		return $settings;
	}
}
