<?php
/**
 * Versioned Schema.org / Google support metadata for schema types.
 *
 * @package SEOvaultSEO\Schema\Validation
 */

namespace SEOvaultSEO\Schema\Validation;

use SEOvaultSEO\Schema\SchemaTypePolicyRegistry;

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

/**
 * Reads support status from the versioned Google rule pack without inventing
 * rich-result promises in UI strings.
 */
class SchemaTypeSupportRegistry {

	/**
	 * Cached pack.
	 *
	 * @var array|null
	 */
	private static $pack = null;

	/**
	 * Reset cache (tests).
	 *
	 * @return void
	 */
	public static function reset_cache() {
		self::$pack = null;
	}

	/**
	 * Get support metadata for a canonical type.
	 *
	 * @param string $type Schema type.
	 * @return array{
	 *   type:string,
	 *   documentation_key:string,
	 *   google_support_status:string,
	 *   source_url:string,
	 *   schema_org_url:string,
	 *   completeness:string,
	 *   version:string,
	 *   checked_at:string
	 * }|null
	 */
	public static function get_entry( $type ) {
		$type = (string) $type;
		if ( '' === $type ) {
			return null;
		}

		$pack  = self::get_pack();
		$rules = isset( $pack['rules'] ) && is_array( $pack['rules'] ) ? $pack['rules'] : array();
		$rule  = isset( $rules[ $type ] ) && is_array( $rules[ $type ] ) ? $rules[ $type ] : array();

		$policy = SchemaTypePolicyRegistry::get_entry( $type );
		$doc_key = '';
		if ( is_array( $policy ) && ! empty( $policy['documentation_key'] ) ) {
			$doc_key = (string) $policy['documentation_key'];
		}
		if ( '' === $doc_key && ! empty( $rule ) ) {
			$doc_key = strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $type ) );
		}

		$status = isset( $rule['google_support_status'] ) ? sanitize_key( (string) $rule['google_support_status'] ) : '';
		if ( '' === $status && empty( $rule ) ) {
			return null;
		}
		if ( '' === $status ) {
			$status = 'partial';
		}

		return array(
			'type'                   => $type,
			'documentation_key'      => $doc_key,
			'google_support_status'  => $status,
			'source_url'             => isset( $rule['source_url'] ) ? (string) $rule['source_url'] : '',
			'schema_org_url'         => isset( $rule['schema_org_url'] ) ? (string) $rule['schema_org_url'] : '',
			'completeness'           => isset( $rule['completeness'] ) ? (string) $rule['completeness'] : '',
			'version'                => isset( $pack['version'] ) ? (string) $pack['version'] : '',
			'checked_at'             => isset( $pack['checked_at'] ) ? (string) $pack['checked_at'] : '',
		);
	}

	/**
	 * Author-facing support copy for Review schema.
	 *
	 * @param string $type Schema type.
	 * @return array{summary:string,learn_more_url:string,status:string}|null
	 */
	public static function get_author_copy( $type ) {
		$entry = self::get_entry( $type );
		if ( null === $entry ) {
			return null;
		}

		$status = (string) $entry['google_support_status'];
		$summary = '';

		switch ( $status ) {
			case 'limited':
				if ( 'FAQPage' === $type ) {
					$summary = __( 'FAQ follows Schema.org FAQPage. Google FAQ rich results are limited and not guaranteed.', 'seovault-ai' );
				} else {
					$summary = __( 'This schema type has limited Google rich-result support and is not a guaranteed result.', 'seovault-ai' );
				}
				break;
			case 'unsupported':
			case 'not_eligible':
				if ( 'HowTo' === $type ) {
					$summary = __( 'How-to follows Schema.org HowTo. Google no longer shows How-to rich results for this markup.', 'seovault-ai' );
				} else {
					$summary = __( 'This markup is valid Schema.org but is not eligible for a Google rich result in current documentation.', 'seovault-ai' );
				}
				break;
			case 'supported':
			case 'partial':
			default:
				$summary = __( 'Local checks use versioned Google documentation. Passing them does not guarantee a rich result.', 'seovault-ai' );
				break;
		}

		$url = '';
		if ( '' !== $entry['documentation_key'] ) {
			$url = ValidationDocumentationRegistry::resolve( $entry['documentation_key'] );
		}
		if ( '' === $url && '' !== $entry['source_url'] ) {
			$url = ValidationDocumentationRegistry::resolve_raw_url( $entry['source_url'] );
		}

		return array(
			'summary'        => $summary,
			'learn_more_url' => $url,
			'status'         => $status,
		);
	}

	/**
	 * Load the versioned rule pack.
	 *
	 * @return array
	 */
	private static function get_pack() {
		if ( null !== self::$pack ) {
			return self::$pack;
		}

		$data = require __DIR__ . '/Data/GoogleRichResultRules.php';
		if ( ! is_array( $data ) ) {
			$data = array(
				'version'    => '',
				'checked_at' => '',
				'rules'      => array(),
			);
		}

		self::$pack = $data;
		return self::$pack;
	}
}
