<?php
/**
 * Mutable execution context for a single workflow run.
 *
 * Actions read and write {@see Context::$data} so values (e.g. generated coupons)
 * are visible to later steps without relying on PHP array pass-by-reference quirks.
 *
 * @package CartCue
 */

namespace CartCue;

defined( 'ABSPATH' ) || exit;

final class Context {

	/**
	 * Trigger payload plus any keys injected during the run (coupon_code, etc.).
	 *
	 * @var array<string, mixed>
	 */
	public array $data;

	public function __construct( array $data = [] ) {
		$this->data = $data;
	}

	/** Return the full data array (used by WorkflowRunner for logging). */
	public function all(): array {
		return $this->data;
	}

	/** Get a single key with optional default. */
	public function get( string $key, mixed $default = null ): mixed {
		return $this->data[$key] ?? $default;
	}

	/** Write a value into context (e.g. coupon_code for later actions). */
	public function set( string $key, mixed $value ): void {
		$this->data[$key] = $value;
	}
}
