<?php
/**
 * Centralized plugin compatibility info.
 *
 * Each entry describes how well CWPM scans a third-party plugin's media
 * references and what the user should be aware of.
 *
 * @package CoffeeWP_Media_Cleanup
 */

namespace CWPM\Compat;

defined( 'ABSPATH' ) || exit;

class PluginCompat {

	/**
	 * Get compatibility info for all detected plugins.
	 *
	 * @return array[] Each entry: [ 'name' => '', 'status' => 'supported|partial|unsupported', 'message' => '' ]
	 */
	public static function get_detected() {

		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$checks = array(
			array(
				'name'    => 'ACF',
				'detect'  => function () {
					return class_exists( 'ACF' )
						|| class_exists( 'acf' )
						|| is_plugin_active( 'advanced-custom-fields/acf.php' )
						|| is_plugin_active( 'advanced-custom-fields-pro/acf.php' );
				},
				'status'  => 'supported',
				'message' => __( 'ACF fields are scanned via post meta. Image, gallery, and file fields are detected.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'WooCommerce',
				'detect'  => function () {
					return class_exists( 'WooCommerce' )
						|| is_plugin_active( 'woocommerce/woocommerce.php' );
				},
				'status'  => 'supported',
				'message' => __( 'Product galleries, category thumbnails, variation images, and placeholder images are scanned.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'Divi',
				'detect'  => function () {
					return class_exists( 'ET_Builder_Plugin' )
						|| function_exists( 'et_setup_theme' )
						|| is_plugin_active( 'divi-builder/divi-builder.php' );
				},
				'status'  => 'partial',
				'message' => __( 'Divi stores media in shortcode attributes. Post content is scanned but some module-specific references may be missed.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'Beaver Builder',
				'detect'  => function () {
					return class_exists( 'FLBuilderLoader' )
						|| is_plugin_active( 'bb-plugin/fl-builder.php' );
				},
				'status'  => 'unsupported',
				'message' => __( 'Beaver Builder stores media in serialized post meta. Deep scanning is not yet implemented — review results carefully before cleaning.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'WPBakery',
				'detect'  => function () {
					return class_exists( 'Vc_Manager' )
						|| is_plugin_active( 'js_composer/js_composer.php' );
				},
				'status'  => 'unsupported',
				'message' => __( 'WPBakery stores media in shortcode attributes. Deep scanning is not yet implemented — review results carefully before cleaning.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'Oxygen Builder',
				'detect'  => function () {
					return function_exists( 'ct_setup_builder' )
						|| is_plugin_active( 'oxygen/ct-oxygen-plugin.php' );
				},
				'status'  => 'unsupported',
				'message' => __( 'Oxygen Builder stores media in custom JSON structures. Deep scanning is not yet implemented — review results carefully before cleaning.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'Brizy',
				'detect'  => function () {
					return class_exists( 'Brizy_Editor' )
						|| is_plugin_active( 'brizy/brizy.php' );
				},
				'status'  => 'unsupported',
				'message' => __( 'Brizy stores media in its own data format. Deep scanning is not yet implemented — review results carefully before cleaning.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'Avada (Fusion)',
				'detect'  => function () {
					return class_exists( 'FusionBuilder' )
						|| is_plugin_active( 'fusion-builder/fusion-builder.php' );
				},
				'status'  => 'unsupported',
				'message' => __( 'Avada Fusion Builder stores media in custom post meta. Deep scanning is not yet implemented — review results carefully before cleaning.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'SiteOrigin',
				'detect'  => function () {
					return class_exists( 'SiteOrigin_Panels' )
						|| is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' );
				},
				'status'  => 'unsupported',
				'message' => __( 'SiteOrigin Panels stores media in serialized post meta. Deep scanning is not yet implemented — review results carefully before cleaning.', 'coffeewp-media-cleanup' ),
			),
			array(
				'name'    => 'Bricks Builder',
				'detect'  => function () {
					return class_exists( 'Bricks\Theme' );
				},
				'status'  => 'unsupported',
				'message' => __( 'Bricks Builder stores media in custom post meta. Deep scanning is not yet implemented — review results carefully before cleaning.', 'coffeewp-media-cleanup' ),
			),
		);

		$plugins = array();

		foreach ( $checks as $check ) {
			if ( call_user_func( $check['detect'] ) ) {
				$plugins[] = array(
					'name'    => $check['name'],
					'status'  => $check['status'],
					'message' => isset( $check['message'] ) ? $check['message'] : '',
				);
			}
		}

		return $plugins;
	}

	/**
	 * Return all detected plugins for the admin "compat notice" grid,
	 * enriched with a `state` (supported|partial|unsupported) and human-readable
	 * `label`.
	 *
	 * Other plugins / extensions may filter the per-plugin state via the
	 * `cwpm_compat_plugin_state` filter — e.g. to mark a state as partial
	 * when extra deep parsers are needed for a given integration.
	 *
	 * @return array[]
	 */
	public static function get_for_notice() {
		$detected = self::get_detected();

		$slug_map = array(
			'ACF'            => 'acf',
			'WooCommerce'    => 'woocommerce',
			'Divi'           => 'divi',
			'Beaver Builder' => 'beaver-builder',
			'WPBakery'       => 'wpbakery',
			'Oxygen Builder' => 'oxygen',
			'Brizy'          => 'brizy',
			'Avada (Fusion)' => 'avada',
			'SiteOrigin'     => 'siteorigin',
			'Bricks Builder' => 'bricks',
		);

		$out = array();
		foreach ( $detected as $entry ) {
			$slug   = isset( $slug_map[ $entry['name'] ] )
				? $slug_map[ $entry['name'] ]
				: sanitize_title( $entry['name'] );
			$status = isset( $entry['status'] ) ? $entry['status'] : 'supported';

			if ( 'partial' === $status ) {
				$state = 'partial';
				$label = __( 'Partial', 'coffeewp-media-cleanup' );
			} elseif ( 'unsupported' === $status ) {
				$state = 'unsupported';
				$label = __( 'Unsupported', 'coffeewp-media-cleanup' );
			} else {
				$state = 'supported';
				$label = __( 'Supported', 'coffeewp-media-cleanup' );
			}

			/**
			 * Filter the per-plugin compat-notice state.
			 *
			 * Extension plugins may use this to override the rendered state
			 * for a given integration. Returned value is passed through to
			 * the JS renderer as `data-state` on the cell, allowing custom
			 * styling via CSS selectors.
			 *
			 * @param string $state One of supported|partial|unsupported.
			 * @param string $name  Plugin display name (e.g. 'Elementor').
			 */
			$state = apply_filters( 'cwpm_compat_plugin_state', $state, $entry['name'] );

			$out[] = array(
				'slug'  => $slug,
				'name'  => $entry['name'],
				'state' => $state,
				'label' => $label,
			);
		}
		return $out;
	}
}
