<?php
/**
 * Independent unfinished-schema setup status vocabulary.
 *
 * @package SEOvaultSEO\Schema\Validation
 */

namespace SEOvaultSEO\Schema\Validation;

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

/**
 * Labels for unfinished/withheld schema setup, separate from published health.
 */
class SchemaSetupStatus {

	const DRAFT             = 'draft';
	const NEEDS_INFORMATION = 'needs_information';
	const READY             = 'ready';
	const WITHHELD          = 'withheld';
	const INVALID           = 'invalid';
	const EMITTED           = 'emitted';

	/**
	 * Resolve one withheld descriptor into a setup status.
	 *
	 * @param array $item Withheld descriptor from validation setup payload.
	 * @return string
	 */
	public static function resolve_withheld_item( array $item ) {
		$status = isset( $item['status'] ) ? (string) $item['status'] : '';
		$reason = isset( $item['reason'] ) ? (string) $item['reason'] : '';

		if ( self::DRAFT === $status || 'draft' === $reason ) {
			return self::DRAFT;
		}
		if ( self::READY === $status || 'ready' === $reason ) {
			return self::READY;
		}
		if ( in_array( $reason, array( 'specialized_not_ready', 'foundational_not_ready', 'faq_block_incomplete' ), true ) ) {
			return self::NEEDS_INFORMATION;
		}
		if ( self::WITHHELD === $status || '' !== $reason ) {
			return self::WITHHELD;
		}

		return self::NEEDS_INFORMATION;
	}

	/**
	 * Resolve custom JSON setup status.
	 *
	 * @param array|null $custom Custom JSON setup payload.
	 * @return string
	 */
	public static function resolve_custom_json( $custom ) {
		if ( ! is_array( $custom ) || empty( $custom['status'] ) ) {
			return '';
		}

		$status = (string) $custom['status'];
		if ( self::EMITTED === $status ) {
			return self::EMITTED;
		}
		if ( 'invalid' === $status ) {
			return self::INVALID;
		}
		if ( self::WITHHELD === $status ) {
			return self::WITHHELD;
		}

		return $status;
	}

	/**
	 * Whether setup needs a primary author action.
	 *
	 * @param string $status Setup status.
	 * @return bool
	 */
	public static function needs_action( $status ) {
		return in_array(
			(string) $status,
			array( self::NEEDS_INFORMATION, self::WITHHELD, self::INVALID, self::DRAFT ),
			true
		);
	}
}
