<?php
/**
 * Incremental retention maintenance.
 *
 * @package LocalGuardScanner
 */

namespace LocalGuardScanner\Application\Maintenance;

use LocalGuardScanner\Infrastructure\Database\FindingRepository;
use LocalGuardScanner\Infrastructure\Database\InventoryRepository;
use LocalGuardScanner\Infrastructure\Database\ScanRepository;

/**
 * Removes only old terminal history in small bounded batches.
 */
final class RetentionCleaner {
	/** @var ScanRepository */
	private $scans;
	/** @var FindingRepository */
	private $findings;
	/** @var InventoryRepository */
	private $inventory;

	public function __construct( ScanRepository $scans, FindingRepository $findings, InventoryRepository $inventory ) {
		$this->scans     = $scans;
		$this->findings  = $findings;
		$this->inventory = $inventory;
	}

	/**
	 * @param string $cutoff UTC MySQL timestamp.
	 * @param int    $limit  Maximum rows per table.
	 * @return array{findings:int,scans:int,inventory:int}
	 */
	public function run( $cutoff, $limit = 250 ) {
		$limit = max( 1, min( 1000, (int) $limit ) );
		return array(
			'findings'  => $this->findings->delete_old_resolved( $cutoff, $limit ),
			'scans'     => $this->scans->delete_old_terminal( $cutoff, 5, $limit ),
			'inventory' => $this->inventory->delete_orphans( $limit ),
		);
	}
}
