<?php
/**
 * Snapshot and rollback safety module boundary.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Rollback;

use ScheduledChanges\Field\FieldRegistry;
use ScheduledChanges\Lock\LockManager;
use ScheduledChanges\Provider\ProviderRegistry;
use ScheduledChanges\Repository\AuditRepository;
use ScheduledChanges\Repository\LaunchVersionRepository;
use ScheduledChanges\Repository\RunRepository;
use ScheduledChanges\Scheduler\EnvironmentFingerprint;
use ScheduledChanges\Snapshot\SnapshotServices;

/**
 * Builds snapshot capture and conflict-aware rollback services.
 */
final class RollbackModule {
	public const MODULE_ID = 'scheduled-changes-snapshots-rollback';

	/**
	 * Creates compilation of the immutable post-launch rollback boundary.
	 */
	public function createEndVersionCompiler( LaunchVersionRepository $versions ): EndVersionCompiler {
		return new EndVersionCompiler( $versions );
	}

	/**
	 * Creates the recovery service after execution supplies its lock contract.
	 *
	 * @param CreatedTargetRollbackHandler[] $created_target_handlers Provider-owned created-target handlers.
	 */
	public function createRollbackService(
		SnapshotServices $snapshots,
		LaunchVersionRepository $versions,
		RunRepository $runs,
		ProviderRegistry $providers,
		FieldRegistry $fields,
		LockManager $locks,
		AuditRepository $audit,
		array $created_target_handlers = array(),
		?EnvironmentFingerprint $environment = null
	): RollbackService {
		return new RollbackService(
			$versions,
			$runs,
			$snapshots->repository(),
			$providers,
			$fields,
			$locks,
			$audit,
			$snapshots->coordinator(),
			$created_target_handlers,
			$environment
		);
	}
}
