<?php
/**
 * Derived launch health projection.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Domain\Launch;

/**
 * Carries shared user-visible health, actions, and diagnostics.
 */
final class LaunchHealthProjection {
	private string $health;
	private string $reason;
	/**
	 * @var string[]
	 */
	private array $available_actions;
	/**
	 * @var array<int,array<string,string>>
	 */
	private array $diagnostics;

	/**
	 * @param string[]                        $available_actions Available action IDs.
	 * @param array<int,array<string,string>> $diagnostics Diagnostic rows.
	 */
	public function __construct( string $health, string $reason, array $available_actions = array(), array $diagnostics = array() ) {
		$this->health            = $health;
		$this->reason            = $reason;
		$this->available_actions = array_values( array_unique( $available_actions ) );
		$this->diagnostics       = array_values( $diagnostics );
	}

	/**
	 * Returns the derived health value.
	 */
	public function health(): string {
		return $this->health;
	}

	/**
	 * Returns the primary reason code.
	 */
	public function reason(): string {
		return $this->reason;
	}

	/**
	 * Returns whether a named action is currently available.
	 */
	public function can( string $action ): bool {
		return in_array( $action, $this->available_actions, true );
	}

	/**
	 * @return string[]
	 */
	public function availableActions(): array {
		return $this->available_actions;
	}

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

	/**
	 * @return array<string,mixed>
	 */
	public function toArray(): array {
		return array(
			'health'           => $this->health,
			'healthReason'     => $this->reason,
			'availableActions' => $this->available_actions,
			'diagnostics'      => $this->diagnostics,
		);
	}
}
