<?php
/**
 * Schema conflict detection service
 *
 * Detects other plugins that emit schema.org markup alongside Solomon Schema.
 * Yoast SEO is the primary case — it runs on every site we build, and its
 * graph (Organization, WebSite, BreadcrumbList…) duplicates ours. Solomon
 * Schema can suppress Yoast's schema output while leaving the rest of Yoast
 * (titles, meta, sitemaps) untouched.
 *
 * @package BW_Schema
 * @since 3.2.0
 */

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

class BW_Schema_Service_Conflicts {

	/**
	 * Option storing whether Yoast's schema output is suppressed ('1'|'0')
	 *
	 * @var string
	 */
	const YOAST_OPTION = 'bw_schema_disable_yoast_schema';

	/**
	 * Whether Yoast SEO is active
	 *
	 * @return bool
	 */
	public static function is_yoast_active() {
		return defined( 'WPSEO_VERSION' );
	}

	/**
	 * Yoast SEO version string, if active
	 *
	 * @return string
	 */
	public static function get_yoast_version() {
		return defined( 'WPSEO_VERSION' ) ? WPSEO_VERSION : '';
	}

	/**
	 * Whether Yoast's schema output should be suppressed
	 *
	 * Defaults to suppressed: Yoast runs alongside every build for SEO meta,
	 * but Solomon Schema owns structured data. Two Organization graphs on
	 * the same page give search engines conflicting entities.
	 *
	 * @return bool
	 */
	public static function is_yoast_schema_disabled() {
		return '1' === get_option( self::YOAST_OPTION, '1' );
	}

	/**
	 * Enable or disable suppression of Yoast's schema output
	 *
	 * @param bool $disabled True to suppress Yoast schema output
	 * @return void
	 */
	public static function set_yoast_schema_disabled( $disabled ) {
		update_option( self::YOAST_OPTION, $disabled ? '1' : '0' );
	}

	/**
	 * Detect other known schema-emitting plugins (informational)
	 *
	 * Solomon Schema has no suppression switch for these — if one is found,
	 * the site owner should deactivate it or disable its schema module.
	 *
	 * @return array[] { name: string, detail: string }
	 */
	public static function detect_other_schema_plugins() {
		$found = array();

		if ( class_exists( 'RankMath' ) ) {
			$found[] = array(
				'name'   => 'Rank Math SEO',
				'detail' => __( 'Emits its own schema graph. Disable its Schema module (Rank Math → Dashboard → Modules) or deactivate the plugin.', 'bw-schema' ),
			);
		}

		if ( defined( 'AIOSEO_VERSION' ) ) {
			$found[] = array(
				'name'   => 'All in One SEO',
				'detail' => __( 'Emits its own schema graph. Disable its schema output in AIOSEO → Search Appearance, or deactivate the plugin.', 'bw-schema' ),
			);
		}

		if ( defined( 'SEOPRESS_VERSION' ) ) {
			$found[] = array(
				'name'   => 'SEOPress',
				'detail' => __( 'May emit schema via its PRO structured data types. Review SEOPress settings or deactivate the plugin.', 'bw-schema' ),
			);
		}

		if ( class_exists( 'BSF_AIOSRS_Pro_Admin' ) || defined( 'BSF_AIOSRS_PRO_VER' ) ) {
			$found[] = array(
				'name'   => 'Schema Pro',
				'detail' => __( 'A dedicated schema plugin — running it alongside Solomon Schema duplicates markup. Deactivate one of the two.', 'bw-schema' ),
			);
		}

		return $found;
	}
}
