<?php
/**
 * Provider snapshot.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Provider;

use InvalidArgumentException;

/**
 * Canonical provider-owned state captured for diff and rollback.
 */
final class ProviderSnapshot {
	private string $providerId;

	private string $providerVersion;

	/**
	 * @var array<string,mixed>
	 */
	private array $values;

	/**
	 * @param array<string,mixed> $values Canonical field values keyed by path.
	 */
	public function __construct(
		string $providerId,
		string $providerVersion,
		array $values
	) {
		if ( '' === $providerId || '' === $providerVersion ) {
			throw new InvalidArgumentException( 'Snapshot provider identity is required.' );
		}

		ksort( $values );

		$this->providerId      = $providerId;
		$this->providerVersion = $providerVersion;
		$this->values          = $values;
	}

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

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

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

	/**
	 * @return mixed
	 */
	public function value( string $fieldId ) {
		return $this->values[ $fieldId ] ?? null;
	}
}
