<?php
/**
 * Deprecation notices for legacy enabled + show_on specialized global schemas.
 *
 * @package SEOvaultSEO\Schema
 */

namespace SEOvaultSEO\Schema;

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

/**
 * Detects legacy catalogue patterns that V2 replaces with post/block/templates.
 */
class SchemaLegacyGlobalDeprecation {

	/**
	 * Specialized types that must not be globally enabled via enabled + show_on.
	 *
	 * @var string[]
	 */
	const SPECIALIZED_TYPES = array(
		'Product',
		'FAQPage',
		'HowTo',
		'Review',
		'Recipe',
		'Event',
		'Course',
		'SoftwareApplication',
		'JobPosting',
	);

	/**
	 * Scan saved global schemas for deprecated enabled + show_on specialized entries.
	 *
	 * @param mixed $schemas Optional schemas option; defaults to stored option.
	 * @return array{
	 *   has_deprecated:bool,
	 *   entries:array<int,array{id:string,type:string,show_on_rules:string[]}>,
	 *   notice:string
	 * }
	 */
	public static function analyze( $schemas = null ) {
		if ( null === $schemas ) {
			$schemas = get_option( 'seovaultseo_schemas', array() );
		}
		if ( ! is_array( $schemas ) ) {
			$schemas = array();
		}

		$entries = array();
		foreach ( $schemas as $id => $schema ) {
			if ( ! is_array( $schema ) ) {
				continue;
			}
			if ( empty( $schema['enabled'] ) ) {
				continue;
			}

			$type = isset( $schema['type'] ) ? (string) $schema['type'] : '';
			if ( ! in_array( $type, self::SPECIALIZED_TYPES, true ) ) {
				continue;
			}

			$show_on = isset( $schema['show_on'] ) && is_array( $schema['show_on'] ) ? $schema['show_on'] : array();
			$rules   = isset( $show_on['rules'] ) && is_array( $show_on['rules'] ) ? $show_on['rules'] : array();
			if ( empty( $rules ) && empty( $show_on['specific'] ) ) {
				// Still deprecated when enabled without explicit targeting — site-wide intent.
				$rules = array( 'unspecified' );
			}

			$entries[] = array(
				'id'            => is_string( $id ) ? $id : (string) $id,
				'type'          => $type,
				'show_on_rules' => array_map( 'strval', $rules ),
			);
		}

		$notice = '';
		if ( ! empty( $entries ) ) {
			$types = array_values( array_unique( array_map(
				static function ( $entry ) {
					return $entry['type'];
				},
				$entries
			) ) );
			$notice = sprintf(
				/* translators: %s: comma-separated schema types. */
				__( 'Deprecated: globally enabling specialized schema (%s) with enabled + show_on is no longer supported in Schema Architecture V2. Add these types on a post, SEOvault block, or targeted template instead. Use migration review if you need to convert the previous configuration.', 'seovault-ai' ),
				implode( ', ', $types )
			);
		}

		return array(
			'has_deprecated' => ! empty( $entries ),
			'entries'        => $entries,
			'notice'         => $notice,
		);
	}

	/**
	 * Whether a deprecation notice should appear in V2 Schema settings.
	 *
	 * @return bool
	 */
	public static function should_show_notice() {
		$analysis = self::analyze();
		return ! empty( $analysis['has_deprecated'] );
	}

	/**
	 * Notice copy for UI (empty when nothing to show).
	 *
	 * @return string
	 */
	public static function notice_copy() {
		if ( ! self::should_show_notice() ) {
			return '';
		}
		$analysis = self::analyze();
		return isset( $analysis['notice'] ) ? (string) $analysis['notice'] : '';
	}
}
