<?php
/**
 * Schema-specific competing-plugin compatibility policy (Architecture V2).
 *
 * Competitors never rewrite third-party blocks or schema. Output gating follows
 * SEOvault output-mode only; duplicates are reported, not silently merged.
 *
 * @package SEOvaultSEO\Schema
 */

namespace SEOvaultSEO\Schema;

use SEOvaultSEO\Compatibility\Detector;
use SEOvaultSEO\Core\Options;

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

/**
 * Compatibility status and output-mode matrix for schema emission.
 */
class SchemaCompatibilityPolicy {

	/**
	 * Types watched for public live-page duplication with competitors.
	 *
	 * @var string[]
	 */
	const WATCHED_DUPLICATE_TYPES = array(
		'Article',
		'BlogPosting',
		'NewsArticle',
		'Product',
		'FAQPage',
	);

	/**
	 * Full schema compatibility status for UI and diagnostics.
	 *
	 * @return array{
	 *   output_mode:string,
	 *   should_output_schema:bool,
	 *   seovault_emits:bool,
	 *   competitors:string[],
	 *   detected_competitor:string,
	 *   duplicate_risk:string,
	 *   recommendation:string,
	 *   rewrites_third_party:bool,
	 *   matrix:array<int,array>
	 * }
	 */
	public static function get_status() {
		$detector    = Detector::get_status();
		$output_mode = Options::get_output_mode();
		$competitors = isset( $detector['competitors'] ) && is_array( $detector['competitors'] )
			? array_values( array_filter( $detector['competitors'], 'is_string' ) )
			: array();
		$has_competitors = ! empty( $competitors );
		$should_output   = Detector::should_output_seo() && Options::is_schema_enabled();

		$duplicate_risk = 'none';
		if ( $has_competitors && $should_output ) {
			$duplicate_risk = 'high';
		} elseif ( $has_competitors && ! $should_output ) {
			$duplicate_risk = 'avoided';
		}

		$recommendation = __( 'SEOvault manages schema output for this site.', 'seovault-ai' );
		if ( $has_competitors && $should_output ) {
			$recommendation = sprintf(
				/* translators: %s: competing plugin names. */
				__( 'Another SEO plugin was detected (%s). SEOvault still outputs its own schema when structured data is enabled — review both outputs (especially Article, Product, and FAQ) to avoid duplicates. Set output mode to Disabled if the other plugin should own schema.', 'seovault-ai' ),
				implode( ', ', $competitors )
			);
		} elseif ( $has_competitors && ! $should_output ) {
			$recommendation = sprintf(
				/* translators: %s: competing plugin names. */
				__( 'Another SEO plugin was detected (%s). SEOvault schema output is currently off, so SEOvault will not double-publish structured data.', 'seovault-ai' ),
				implode( ', ', $competitors )
			);
		}

		return array(
			'output_mode'           => $output_mode,
			'should_output_schema'  => $should_output,
			'seovault_emits'        => $should_output,
			'competitors'           => $competitors,
			'detected_competitor'   => isset( $detector['detected_competitor'] ) ? (string) $detector['detected_competitor'] : Detector::PROVIDER_NONE,
			'duplicate_risk'        => $duplicate_risk,
			'recommendation'        => $recommendation,
			'rewrites_third_party'  => false,
			'watched_types'         => self::WATCHED_DUPLICATE_TYPES,
			'matrix'                => self::output_mode_matrix( $has_competitors ),
		);
	}

	/**
	 * Competing-plugin × output-mode matrix (schema emission behavior).
	 *
	 * @param bool $has_competitors Whether any competitor is active.
	 * @return array<int,array{mode:string,competitor:string,seovault_emits:bool,duplicate_risk:string,notes:string}>
	 */
	public static function output_mode_matrix( $has_competitors = true ) {
		$competitor_label = $has_competitors ? 'present' : 'none';

		return array(
			array(
				'mode'            => 'auto',
				'competitor'      => $competitor_label,
				'seovault_emits'  => true,
				'duplicate_risk'  => $has_competitors ? 'high' : 'none',
				'notes'           => 'SEOvault emits schema; competitors informational only.',
			),
			array(
				'mode'            => 'manual',
				'competitor'      => $competitor_label,
				'seovault_emits'  => true,
				'duplicate_risk'  => $has_competitors ? 'high' : 'none',
				'notes'           => 'SEOvault emits schema; SEO meta may be manual while schema still outputs.',
			),
			array(
				'mode'            => 'disabled',
				'competitor'      => $competitor_label,
				'seovault_emits'  => false,
				'duplicate_risk'  => 'avoided',
				'notes'           => 'SEOvault schema and SEO tags are suppressed; other plugin may own output.',
			),
		);
	}

	/**
	 * Resolve matrix row for the current site state.
	 *
	 * @return array{mode:string,competitor:string,seovault_emits:bool,duplicate_risk:string,notes:string}|null
	 */
	public static function current_matrix_row() {
		$status = self::get_status();
		$has    = ! empty( $status['competitors'] );
		foreach ( self::output_mode_matrix( $has ) as $row ) {
			if ( $row['mode'] === $status['output_mode'] ) {
				return $row;
			}
		}
		return null;
	}
}
