<?php
/**
 * Platform Integration Functions
 *
 * WooCommerce/EasyCommerce specific functions
 *
 * @since   3.0.0
 * @package WC_Affiliate
 */

/**
 * Get Ncrypt encryption instance.
 *
 * @return \mukto90\Ncrypt Encryption instance.
 */
function wca_ncrypt(): \mukto90\Ncrypt {
	return new \mukto90\Ncrypt();
}

/**
 * Install and activate a WordPress plugin
 *
 * @param string $plugin_slug The plugin slug (e.g., 'woocommerce')
 * @param string $plugin_file The plugin file path (e.g., 'woocommerce/woocommerce.php')
 *
 * @return bool|WP_Error Response array with success status and message
 */
function wca_install_plugin( $plugin_slug, $plugin_file ) {
	// Include required files if not already loaded
	if ( ! function_exists( 'plugins_api' ) ) {
		require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
	}
	if ( ! class_exists( 'WP_Upgrader' ) ) {
		require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
	}
	if ( ! function_exists( 'request_filesystem_credentials' ) ) {
		require_once ABSPATH . 'wp-admin/includes/file.php';
	}

	// Get plugin information from WordPress.org
	$api = plugins_api(
		'plugin_information',
		array(
			'slug' => $plugin_slug,
		)
	);

	if ( is_wp_error( $api ) ) {
		return $api;
	}

	// Install the plugin
	$upgrader       = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() );
	$install_result = $upgrader->install( $api->download_link );

	if ( is_wp_error( $install_result ) ) {
		return $install_result;
	}

	return true;
}
