<?php
/**
 * Side-effect-free operation plan dry run.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Validation;

use ScheduledChanges\Domain\Operation\OperationGraph;

/**
 * Runs full validation and projects dependency-safe intended writes.
 */
final class DryRunService {
	private PlanValidator $validator;
	public function __construct( PlanValidator $validator ) {
		$this->validator = $validator;
	}

	/**
	 * @param array<string,string> $requiredProviders Exact provider versions.
	 */
	public function run(
		OperationGraph $graph,
		array $requiredProviders,
		int $actor_id,
		bool $includeInfrastructure = true
	): DryRunResult {
		$validation = $this->validator->validate(
			$graph,
			$requiredProviders,
			$actor_id,
			$includeInfrastructure
		);

		if ( ! $validation->isValid() ) {
			return new DryRunResult( $validation, array() );
		}

		return new DryRunResult(
			$validation,
			array_map(
				static fn ( $operation ): DryRunOperation => DryRunOperation::fromOperation( $operation ),
				$graph->topologicalOrder()
			)
		);
	}
}
