<?php
/**
 * Records the one review-prompt success signal that originates outside the
 * admin (a blocked card-testing attack), so the admin review notice can
 * celebrate it later.
 *
 * The card-testing block fires on the frontend and the lockdown state is a
 * short-lived (90s) transient, so a durable, set-once option is needed to
 * remember that an attack was ever stopped. The other success triggers
 * (protected revenue, surfaced risk) are read at render time from existing
 * getters and need no listener.
 *
 * @package TrustLens
 * @since   1.3.2
 */

defined( 'ABSPATH' ) || exit;

/**
 * Always-loaded listener that persists the "attack blocked" review signal.
 *
 * @since 1.3.2
 */
class TrustLens_Review_Events {

	/**
	 * Option holding the Unix timestamp of the first blocked card-testing
	 * attack. Prefixed `trustlens_` so it is swept by TrustLens_Data_Purge on
	 * uninstall without an explicit registration.
	 *
	 * @var string
	 */
	const ATTACK_OPTION = 'trustlens_review_event_attack';

	/**
	 * Register hooks. Must run in every context (frontend + cron), not only
	 * admin, because the source event fires during checkout.
	 *
	 * @since 1.3.2
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'trustlens/card_testing/attack_detected', array( $this, 'record_attack' ) );
	}

	/**
	 * Record the first blocked attack (set-once).
	 *
	 * @since 1.3.2
	 * @return void
	 */
	public function record_attack(): void {
		if ( ! get_option( self::ATTACK_OPTION ) ) {
			update_option( self::ATTACK_OPTION, time(), false );
		}
	}
}
