<?php
/**
 * WC_Bankful_Dependency_Manager class
 *
 * Manages the dependency checks for the Bankful plugin, ensuring that WooCommerce
 * is active and meets the minimum version requirement.
 *
 * @package Bankful
 * @since 3.0.2
 */

defined( 'ABSPATH' ) || exit;

class WC_Bankful_Dependency_Manager {

	/**
	 * The minimum required version of WooCommerce.
	 *
	 * @var string
	 */
	private $required_wc_version;

	/**
	 * The currently active version of WooCommerce.
	 *
	 * @var string|null
	 */
	private $current_wc_version = null;

	/**
	 * Indicates whether the WooCommerce version has been cached.
	 *
	 * @var bool
	 */
	private $is_wc_version_cached = false;

	/**
	 * Constructor to initialize the dependency manager.
	 *
	 * @param string $required_wc_version Minimum required WooCommerce version.
	 */
	public function __construct( $required_wc_version ) {
		$this->required_wc_version = $required_wc_version;
	}

	/**
	 * Validates if the necessary dependencies are met.
	 *
	 * @return bool True if both WooCommerce is active and meets the version requirement.
	 */
	public function are_dependencies_valid() {
		return $this->is_woocommerce_active() && $this->is_wc_version_compatible();
	}

	/**
	 * Checks if WooCommerce is currently active.
	 *
	 * @return bool True if WooCommerce is active, false otherwise.
	 */
	private function is_woocommerce_active() {
		return class_exists( 'WooCommerce' ) || $this->fetch_wc_version() !== null;
	}

	/**
	 * Verifies if the active WooCommerce version meets the minimum requirement.
	 *
	 * @return bool True if the active version is equal to or greater than the required version.
	 */
	private function is_wc_version_compatible() {
		return version_compare(
			$this->fetch_wc_version() ? $this->fetch_wc_version() : '',
			$this->required_wc_version,
			'>='
		);
	}

	/**
	 * Retrieves the version number of the currently active WooCommerce installation.
	 *
	 * @return string|null The WooCommerce version if active, null otherwise.
	 */
	private function fetch_wc_version() {
		if ( defined( 'WC_VERSION' ) ) {
			return WC_VERSION;
		}

		if ( $this->is_wc_version_cached ) {
			return $this->current_wc_version;
		}

		$this->is_wc_version_cached = true;
		$this->current_wc_version = $this->detect_wc_version();

		return $this->current_wc_version;
	}

	/**
	 * Detects the version of WooCommerce by checking active plugins.
	 *
	 * @return string|null The version of WooCommerce, or null if not found.
	 */
	private function detect_wc_version() {
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		foreach ( get_plugins() as $plugin_slug => $plugin_data ) {
			if ( $this->is_woocommerce_plugin( $plugin_slug, $plugin_data ) && is_plugin_active( $plugin_slug ) ) {
				return $plugin_data['Version'];
			}
		}

		return null;
	}

	/**
	 * Determines if the specified plugin data corresponds to WooCommerce.
	 *
	 * @param string $plugin_slug Plugin file path.
	 * @param array  $plugin_data Plugin metadata.
	 * @return bool True if the plugin is WooCommerce.
	 */
	private function is_woocommerce_plugin( $plugin_slug, $plugin_data ) {
		return 'woocommerce/woocommerce.php' === $plugin_slug ||
			( 'woocommerce.php' === basename( $plugin_slug ) && 'WooCommerce' === $plugin_data['Name'] );
	}

	/**
	 * Displays an admin notice if the required plugins are not installed or activated.
	 * 
	 * @return bool False if dependencies are not met, otherwise true.
	 */
	public function show_dependency_admin_notice() {
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return true; // Allow if the user doesn't have permission to activate plugins.
		}

		$message = '';

		if ( ! $this->is_woocommerce_active() ) {
			$install_url = wp_nonce_url(
				add_query_arg(
					array(
						'action' => 'install-plugin',
						'plugin' => 'woocommerce',
					),
					admin_url( 'update.php' )
				),
				'install-plugin_woocommerce'
			);

			$message = sprintf(
				// translators: $1: opening <strong> tag, $2: closing </strong> tag, $3: opening <a> tag , $4: closing </a> tag, $5: opening <a> tag , $6: closing </a> tag
				esc_html__( '%1$sBankful is currently inactive.%2$s The %3$sWooCommerce plugin%4$s is required for Bankful to function. Please %5$sinstall and activate WooCommerce &raquo;%6$s', 'bankful' ),
				'<strong>', '</strong>',
				'<a href="https://wordpress.org/plugins/woocommerce/">', '</a>',
				'<a href="' . esc_url( $install_url ) . '">', '</a>'
			);
		} elseif ( ! $this->is_wc_version_compatible() ) {
			$message = sprintf(
				// translators: $1: opening <strong> tag, $2: closing </strong> tag, $3: numeric value , $4: opening </a> tag, $5: closing <a> tag 
				esc_html__( '%1$sBankful is currently inactive.%2$s This version of Bankful requires WooCommerce version %3$s or higher. Please %4$supdate WooCommerce to the required version &raquo;%5$s', 'bankful' ),
				'<strong>', '</strong>',
				$this->required_wc_version,
				'<a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">', '</a>'
			);
		}

		if ( $message ) {
			echo '<div class="notice notice-error">';
			echo '<p>' . wp_kses_post( $message ) . '</p>';
			echo '</div>';

			// Return false to indicate that the dependencies are not met.
			return false;
		}

		return true;
	}
}
