<?php
if (!defined('ABSPATH')) {
	exit;
}

/**
 * Main ContactsheetsLite namespace.
 *
 * @package contactsheets-lite
 */

/**
 * WPSSLWP Dependency Checker
 */
class WPSSLWP_Dependencies
{
	/**
	 * Active plugins
	 *
	 * @var $active_plugins
	 */
	private static $active_plugins;
	/**
	 * Initialization
	 */
	public static function init()
	{
		if (is_multisite()) {
			if (!function_exists('is_plugin_active_for_network')) {
				require_once ABSPATH . '/wp-admin/includes/plugin.php';
			}
			// Check WPForms active at the network site.
			if (is_plugin_active_for_network('wpforms-lite/wpforms.php')) {
				self::$active_plugins = (array)get_site_option('active_sitewide_plugins', array());
			}
			else { // Check WPForms 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 WPForms exist
	 *
	 * @return Boolean
	 */
	public static function wpsslwp_wpforms_active_check($pro = false)
	{
		if (!self::$active_plugins) {
			self::init();
		}
		if ($pro) {
			if (in_array('wpforms/wpforms.php', self::$active_plugins, true) || array_key_exists('wpforms/wpforms.php', self::$active_plugins)) {
				return true;
			}
		}
		else {
			if (in_array('wpforms-lite/wpforms.php', self::$active_plugins, true) || array_key_exists('wpforms-lite/wpforms.php', self::$active_plugins) || in_array('wpforms/wpforms.php', self::$active_plugins, true) || array_key_exists('wpforms/wpforms.php', self::$active_plugins)) {
				if (!function_exists('is_plugin_active')) {
					require_once ABSPATH . 'wp-admin/includes/plugin.php';
				}
				if (!is_plugin_active('wpsyncsheets-wpforms/wpsyncsheets-lite-wpforms.php')) {
					add_action('admin_notices', 'WPSSLWP_Dependencies::wpsyncsheets_wpsslwp_missing_dependency_notice');
				}
				return true;
			}
		}
		return false;
	}
	/**
	 * Check if WPForms active
	 *
	 * @return Boolean
	 */
	public static function wpsslwp_is_wpforms_plugin_active()
	{
		return self::wpsslwp_wpforms_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,
		];
	}
}