<?php
/**
 * One readiness diagnostic (blocker or improvement).
 *
 * @package SEOvaultSEO\Schema\Readiness
 */

namespace SEOvaultSEO\Schema\Readiness;

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

/**
 * Immutable readiness issue mapped to a field/action key.
 */
class SchemaReadinessIssue {

	const KIND_BLOCKER     = 'blocker';
	const KIND_IMPROVEMENT = 'improvement';

	/**
	 * Stable issue code.
	 *
	 * @var string
	 */
	private $code;

	/**
	 * Issue kind: blocker or improvement.
	 *
	 * @var string
	 */
	private $kind;

	/**
	 * User-understandable field key.
	 *
	 * @var string
	 */
	private $field_key;

	/**
	 * User-understandable action key.
	 *
	 * @var string
	 */
	private $action_key;

	/**
	 * Optional machine path.
	 *
	 * @var string
	 */
	private $path;

	/**
	 * Create an issue from validated readiness data.
	 *
	 * @param array $data Issue data.
	 */
	public function __construct( array $data = array() ) {
		$kind = isset( $data['kind'] ) ? (string) $data['kind'] : self::KIND_BLOCKER;
		if ( self::KIND_IMPROVEMENT !== $kind ) {
			$kind = self::KIND_BLOCKER;
		}

		$this->code       = isset( $data['code'] ) ? (string) $data['code'] : '';
		$this->kind       = $kind;
		$this->field_key  = isset( $data['field_key'] ) ? (string) $data['field_key'] : '';
		$this->action_key = isset( $data['action_key'] ) ? (string) $data['action_key'] : '';
		$this->path       = isset( $data['path'] ) ? (string) $data['path'] : '';
	}

	/**
	 * Get the stable issue code.
	 *
	 * @return string
	 */
	public function get_code() {
		return $this->code;
	}

	/**
	 * Get the issue kind.
	 *
	 * @return string
	 */
	public function get_kind() {
		return $this->kind;
	}

	/**
	 * Whether this issue blocks emission.
	 *
	 * @return bool
	 */
	public function is_blocker() {
		return self::KIND_BLOCKER === $this->kind;
	}

	/**
	 * Get the field key.
	 *
	 * @return string
	 */
	public function get_field_key() {
		return $this->field_key;
	}

	/**
	 * Get the action key.
	 *
	 * @return string
	 */
	public function get_action_key() {
		return $this->action_key;
	}

	/**
	 * Get the optional machine path.
	 *
	 * @return string
	 */
	public function get_path() {
		return $this->path;
	}

	/**
	 * Export a plain array representation.
	 *
	 * @return array
	 */
	public function to_array() {
		return array(
			'code'       => $this->code,
			'kind'       => $this->kind,
			'field_key'  => $this->field_key,
			'action_key' => $this->action_key,
			'path'       => $this->path,
		);
	}
}
