<?php
/**
 * Adaptive batch sizing.
 *
 * @package LocalGuardScanner
 */

namespace LocalGuardScanner\Application\Scan;

/**
 * Adjusts batch size gradually within safe bounds.
 */
final class AdaptiveBatchSizer {
	const MINIMUM = 25;
	const MAXIMUM = 250;

	/**
	 * @param int   $current         Current batch size.
	 * @param float $elapsed_seconds Elapsed batch time.
	 * @param float $target_seconds  Target time slice.
	 * @return int
	 */
	public function next( $current, $elapsed_seconds, $target_seconds = 8.0 ) {
		$current = max( self::MINIMUM, min( self::MAXIMUM, (int) $current ) );
		if ( $elapsed_seconds <= 0.0 ) {
			return min( self::MAXIMUM, $current + 25 );
		}
		$ratio = $target_seconds / $elapsed_seconds;
		$ratio = max( 0.5, min( 1.5, $ratio ) );
		return max( self::MINIMUM, min( self::MAXIMUM, (int) round( $current * $ratio ) ) );
	}
}
