<?php
/**
 * Dry-run result.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Validation;

/**
 * Validation and ordered intended writes without side effects.
 */
final class DryRunResult {
	private ValidationReport $validation;
	/**
	 * @var DryRunOperation[]
	 */
	private array $operations;
	/**
	 * @param DryRunOperation[] $operations Ordered intended operations.
	 */
	public function __construct( ValidationReport $validation, array $operations ) {
		$this->validation = $validation;
		$this->operations = $operations;
	}

	public function validation(): ValidationReport {
		return $this->validation;
	}

	/**
	 * @return DryRunOperation[]
	 */
	public function operations(): array {
		return $this->operations;
	}

	public function executable(): bool {
		return $this->validation->isValid();
	}

	/**
	 * @return array<string,mixed>
	 */
	public function toArray(): array {
		return array(
			'executable' => $this->executable(),
			'validation' => $this->validation->toArray(),
			'operations' => array_map(
				static fn ( DryRunOperation $operation ): array => $operation->toArray(),
				$this->operations
			),
		);
	}
}
