<?php
/**
 * Main WPSyncSheetsWooCommerce namespace.
 *
 * @since 1.0.0
 * @package wpsyncsheets-woocommerce
 */

/**
 * WPSSLW Dependency Checker
 */
class WPSSLW_Dependencies {
	/**
	 * Array of active plugins.
	 *
	 * @var $active_plugins .
	 */
	private static $active_plugins;
	/**
	 * Initialization.
	 *
	 * @since 1.0.0
	 */
	public static function init() {
		if ( is_multisite() ) {
			if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
				require_once ABSPATH . '/wp-admin/includes/plugin.php';
			}
			// Check plugin active at the network site.
			if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
				self::$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
			} else { // Check plugin active at the network individual site.
				self::$active_plugins = (array) get_option( 'active_plugins', array() );
			}
		} else {
			self::$active_plugins = (array) get_option( 'active_plugins', array() );
		}
	}
	/**
	 * Check woocommerce exist
	 *
	 * @return Boolean
	 */
	public static function wpsslw_woocommerce_active_check() {
		if ( ! self::$active_plugins ) {
			self::init();
		}
		return in_array( 'woocommerce/woocommerce.php', self::$active_plugins, true ) || array_key_exists( 'woocommerce/woocommerce.php', self::$active_plugins );
	}
	/**
	 * Check if woocommerce active
	 *
	 * @return Boolean
	 */
	public static function wpsslw_is_woocommerce_active() {
		return self::wpsslw_woocommerce_active_check();
	}
	/**
	 * Check the install and activation status of a plugin in WordPress.
	 *
	 * @param string $plugin_slug Folder name of the plugin.
	 * @param string $plugin_file Full plugin path relative to plugins directory, e.g., 'folder/plugin-file.php'.
	 * @return array Returns an associative array with keys:
	 *               - 'installed' => bool
	 *               - 'active' => bool
	 *               - 'network_active' => bool
	**/
	public static function check_plugin_install_and_activation_status($plugin_slug, $plugin_file) {
		// Ensure required plugin functions are available
		if ( ! function_exists( 'is_plugin_active' ) ) {
			include_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		// Full plugin path: e.g., wp-content/plugins/plugin-folder/plugin-file.php
		$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_file;
		
		// Check if plugin file exists (i.e., plugin is installed)
		$is_installed = file_exists($plugin_path);

		// Check if plugin is active on the current site
		$is_active = is_plugin_active($plugin_file);

		// Check if plugin is network activated (in multisite)
		$is_network_active = is_multisite() && is_plugin_active_for_network($plugin_file);

		return [
			'installed'        => $is_installed,
			'active'           => $is_active,
			'network_active'   => $is_network_active,
		];
	}
}
