<?php
/**
 * Honeypot anti-spam provider.
 *
 * @package Awesomemotive\Rewardswp\AntiSpam\Providers
 * @since 1.2.0
 */

namespace Awesomemotive\Rewardswp\AntiSpam\Providers;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractAntiSpamProvider;

/**
 * Honeypot Provider.
 *
 * Implements honeypot anti-spam protection by adding a hidden field
 * that should remain empty. Bots typically fill all form fields,
 * including the honeypot, revealing themselves as automated.
 *
 * @since 1.2.0
 */
class HoneypotProvider extends AbstractAntiSpamProvider {
	/**
	 * The honeypot field name.
	 *
	 * Uses a name that looks attractive to bots.
	 *
	 * @since 1.2.0
	 *
	 * @var string
	 */
	const FIELD_NAME = 'rwp_website';

	/**
	 * Get the unique provider identifier.
	 *
	 * @since 1.2.0
	 *
	 * @return string The provider identifier.
	 */
	public function get_name(): string {
		return 'honeypot';
	}

	/**
	 * Get the human-readable label for admin display.
	 *
	 * @since 1.2.0
	 *
	 * @return string The provider label.
	 */
	public function get_label(): string {
		return __( 'Honeypot (Invisible)', 'rewardswp' );
	}

	/**
	 * Get the feature slug for license checking.
	 *
	 * @since 1.2.0
	 *
	 * @return string The feature slug.
	 */
	public function get_feature_slug(): string {
		return 'antispam_honeypot';
	}

	/**
	 * Check if the provider is properly configured.
	 *
	 * Honeypot requires no configuration, so it's always ready.
	 *
	 * @since 1.2.0
	 *
	 * @return bool Always true for honeypot.
	 */
	public function is_configured(): bool {
		return true;
	}

	/**
	 * Get the field name used by this provider.
	 *
	 * @since 1.2.0
	 *
	 * @return string The field name.
	 */
	public function get_field_name(): string {
		return self::FIELD_NAME;
	}

	/**
	 * Render the honeypot field.
	 *
	 * Renders a hidden input field that should remain empty.
	 * Uses CSS positioning instead of display:none to avoid bot detection.
	 * The field has an attractive name for bots (website/url).
	 *
	 * @since 1.2.0
	 *
	 * @return void
	 */
	public function render_field(): void {
		?>
		<div style="position:absolute;left:-9999px;top:-9999px;opacity:0;pointer-events:none;" aria-hidden="true">
			<label class="sr-only">
				<?php esc_html_e( 'Leave this field empty', 'rewardswp' ); ?>
			</label>
			<input
				type="text"
				name="<?php echo esc_attr( self::FIELD_NAME ); ?>"
				x-model="formData.<?php echo esc_attr( self::FIELD_NAME ); ?>"
				value=""
				tabindex="-1"
				autocomplete="off"
			>
		</div>
		<?php
	}

	/**
	 * Validate the honeypot submission.
	 *
	 * If the honeypot field is filled, the submission is from a bot.
	 *
	 * @since 1.2.0
	 *
	 * @param array $request_data The request data (typically $_POST).
	 *
	 * @return bool True if validation passed (field is empty), false if spam detected.
	 */
	public function validate( array $request_data ): bool {
		$honeypot_value = $request_data[ self::FIELD_NAME ] ?? '';

		// If the honeypot field is filled, it's a bot.
		if ( ! empty( $honeypot_value ) ) {
			$this->set_error( __( 'Something went wrong. Please try again.', 'rewardswp' ) );
			return false;
		}

		return true;
	}
}

