<?php
/**
 * Provider operation result.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Provider;

/**
 * Explicit result for writes and rollback operations.
 */
final class OperationResult {
	private bool $successful;
	private bool $changed;
	private string $code;
	private string $message;
	/**
	 * @var array<string, mixed>
	 */
	private array $details = array();
	/**
	 * @param array<string,mixed> $details Structured diagnostics.
	 */
	private function __construct( bool $successful, bool $changed, string $code, string $message, array $details = array() ) {
		$this->successful = $successful;
		$this->changed    = $changed;
		$this->code       = $code;
		$this->message    = $message;
		$this->details    = $details;
	}

	/**
	 * @param array<string,mixed> $details Structured diagnostics.
	 */
	public static function success( string $message = '', bool $changed = true, array $details = array() ): self {
		return new self( true, $changed, 'success', $message, $details );
	}

	/**
	 * @param array<string,mixed> $details Structured diagnostics.
	 */
	public static function failure( string $code, string $message, array $details = array() ): self {
		return new self( false, false, $code, $message, $details );
	}

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

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

	public function code(): string {
		return $this->code;
	}

	public function message(): string {
		return $this->message;
	}

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