<?php
/**
 * Velocity_Detector — pure-logic threshold evaluation. Takes pre-computed
 * rolling-window counts and returns whether thresholds are crossed.
 *
 * Separating the window-counting (DB queries) from the threshold check
 * keeps this class unit-testable without database access.
 *
 * @package TrustLens
 * @since   1.2.2
 */

defined( 'ABSPATH' ) || exit;

class TrustLens_Velocity_Detector {

	public const DEFAULT_THRESHOLDS = array(
		'declines_60s'    => 3,   // Short-window declines per fingerprint.
		'submissions_60s' => 10,  // Short-window submissions per fingerprint.
	);

	private array $thresholds;

	public function __construct( array $thresholds = self::DEFAULT_THRESHOLDS ) {
		$this->thresholds = array_merge( self::DEFAULT_THRESHOLDS, $thresholds );
	}

	public function should_target_block( array $counts ): bool {
		$declines_60s    = (int) ( $counts['declines_60s']    ?? 0 );
		$submissions_60s = (int) ( $counts['submissions_60s'] ?? 0 );

		if ( $declines_60s >= $this->thresholds['declines_60s'] ) {
			return true;
		}
		if ( $submissions_60s >= $this->thresholds['submissions_60s'] ) {
			return true;
		}
		return false;
	}
}
