<?php
/**
 * Readiness evaluation result value object.
 *
 * @package SEOvaultSEO\Schema\Readiness
 */

namespace SEOvaultSEO\Schema\Readiness;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Deterministic readiness outcome for one schema entity/instance.
 */
class SchemaReadinessResult {

	/**
	 * Evaluated schema type.
	 *
	 * @var string
	 */
	private $type;

	/**
	 * Policy version used for this result.
	 *
	 * @var string
	 */
	private $policy_version;

	/**
	 * Blockers.
	 *
	 * @var SchemaReadinessIssue[]
	 */
	private $blockers = array();

	/**
	 * Improvements.
	 *
	 * @var SchemaReadinessIssue[]
	 */
	private $improvements = array();

	/**
	 * Create a readiness result.
	 *
	 * @param string                 $type           Schema type.
	 * @param string                 $policy_version Policy version.
	 * @param SchemaReadinessIssue[] $blockers       Blockers.
	 * @param SchemaReadinessIssue[] $improvements   Improvements.
	 */
	public function __construct( $type, $policy_version, array $blockers = array(), array $improvements = array() ) {
		$this->type           = (string) $type;
		$this->policy_version = (string) $policy_version;
		foreach ( $blockers as $issue ) {
			if ( $issue instanceof SchemaReadinessIssue && $issue->is_blocker() ) {
				$this->blockers[] = $issue;
			}
		}
		foreach ( $improvements as $issue ) {
			if ( $issue instanceof SchemaReadinessIssue && ! $issue->is_blocker() ) {
				$this->improvements[] = $issue;
			}
		}
	}

	/**
	 * Whether the entity is ready to emit.
	 *
	 * @return bool
	 */
	public function is_ready() {
		return empty( $this->blockers );
	}

	/**
	 * Get the evaluated type.
	 *
	 * @return string
	 */
	public function get_type() {
		return $this->type;
	}

	/**
	 * Get the policy version.
	 *
	 * @return string
	 */
	public function get_policy_version() {
		return $this->policy_version;
	}

	/**
	 * Get blocker issues.
	 *
	 * @return SchemaReadinessIssue[]
	 */
	public function get_blockers() {
		return $this->blockers;
	}

	/**
	 * Get improvement issues.
	 *
	 * @return SchemaReadinessIssue[]
	 */
	public function get_improvements() {
		return $this->improvements;
	}

	/**
	 * Export a plain array representation.
	 *
	 * @return array
	 */
	public function to_array() {
		return array(
			'type'           => $this->type,
			'ready'          => $this->is_ready(),
			'policy_version' => $this->policy_version,
			'blockers'       => array_map(
				static function ( SchemaReadinessIssue $issue ) {
					return $issue->to_array();
				},
				$this->blockers
			),
			'improvements'   => array_map(
				static function ( SchemaReadinessIssue $issue ) {
					return $issue->to_array();
				},
				$this->improvements
			),
		);
	}
}
