<?php
/**
 * Runtime field conflict decision.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Execution;

/**
 * Describes whether execution should proceed and which canonical value to use.
 */
final class ConflictDecision {
	private bool $proceed;
	/**
	 * @var mixed
	 */
	private $desiredValue;
	private ?OperationExecutionResult $result;
	/**
	 * @var array<string, mixed>
	 */
	private array $details;
	/**
	 * @param array<string,mixed> $details Conflict comparison details.
	 * @param mixed               $desiredValue
	 */
	private function __construct( bool $proceed, $desiredValue, ?OperationExecutionResult $result, array $details ) {
		$this->proceed      = $proceed;
		$this->desiredValue = $desiredValue;
		$this->result       = $result;
		$this->details      = $details;
	}

	/**
	 * @param array<string,mixed> $details Conflict comparison details.
	 * @param mixed               $desiredValue
	 */
	public static function proceed( $desiredValue, array $details ): self {
		return new self( true, $desiredValue, null, $details );
	}

	/**
	 * @param array<string,mixed> $details Conflict comparison details.
	 */
	public static function stop( OperationExecutionResult $result, array $details ): self {
		return new self( false, null, $result, $details );
	}

	public function shouldProceed(): bool {
		return $this->proceed;
	}

	/**
	 * @return mixed
	 */
	public function desiredValue() {
		return $this->desiredValue;
	}

	public function result(): ?OperationExecutionResult {
		return $this->result;
	}

	/**
	 * @return array<string,mixed>
	 */
	public function details(): array {
		return $this->details;
	}
}
