<?php
/**
 * Detects conflicts with other ordering plugins.
 *
 * @package WP_PowerSuite\Ordering
 */

namespace WP_PowerSuite\Ordering;

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

/**
 * Conflict detection for third-party ordering plugins.
 */
final class ConflictDetector {

	/** @var array<string, string> */
	private const KNOWN_PLUGINS = [
		'intuitive-custom-post-order/intuitive-custom-post-order.php' => 'Intuitive Custom Post Order',
		'simple-custom-post-order/simple-custom-post-order.php'       => 'Simple Custom Post Order',
		'taxonomy-terms-order/taxonomy-terms-order.php'               => 'Category Order and Taxonomy Terms Order',
		'post-types-order/post-types-order.php'                       => 'Post Types Order',
		'advanced-post-types-order/advanced-post-types-order.php'     => 'Advanced Post Types Order',
		'wp-term-order/wp-term-order.php'                             => 'WP Term Order',
		'post-type-order/post-type-order.php'                         => 'Post Type Order',
	];

	/**
	 * @return list<string> Human-readable plugin names.
	 */
	public static function getActiveConflicts(): array {
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$conflicts = [];
		foreach ( self::KNOWN_PLUGINS as $plugin_file => $label ) {
			if ( is_plugin_active( $plugin_file ) ) {
				$conflicts[] = $label;
			}
		}

		/**
		 * Filter detected ordering plugin conflicts.
		 *
		 * @param list<string> $conflicts Active conflicting plugin names.
		 */
		return apply_filters( 'wp_powersuite_ordering_conflicts', $conflicts );
	}

	public static function hasConflicts(): bool {
		return self::getActiveConflicts() !== [];
	}

	public static function renderAdminNotice(): void {
		$conflicts = self::getActiveConflicts();
		if ( $conflicts === [] ) {
			return;
		}

		echo '<div class="notice notice-warning wpps-reorder-conflict-notice"' . wp_powersuite_plugin_notice_attr() . '>';
		echo '<p><strong>' . esc_html__( 'Ordering plugin conflict detected', 'wp-powersuite' ) . '</strong></p>';
		echo '<p>' . esc_html(
			sprintf(
				/* translators: %s: comma-separated plugin names */
				__( 'These active plugins also modify content order: %s. Disable one ordering solution to avoid unpredictable results.', 'wp-powersuite' ),
				implode( ', ', $conflicts )
			)
		) . '</p>';
		echo '</div>';
	}

	private static bool $notices_registered = false;

	public static function registerAdminNotices(): void {
		if ( self::$notices_registered ) {
			return;
		}
		self::$notices_registered = true;

		add_action(
			'admin_notices',
			static function (): void {
				if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
					return;
				}

				$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
				if ( ! $screen instanceof \WP_Screen ) {
					return;
				}

				$relevant = in_array( $screen->id, [
					'admin_page_powersuite-custom-post-order',
					'admin_page_powersuite-custom-taxonomy-order',
					'toplevel_page_powersuite',
				], true );

				if ( ! $relevant && $screen->base !== 'post' && $screen->base !== 'edit-tags' ) {
					return;
				}

				self::renderAdminNotice();
			}
		);
	}
}
