<?php
namespace WPUltimateSecurity\UltimateSecurity\Modules\Monitoring\SiteHealth\Collectors;

use WPUltimateSecurity\UltimateSecurity\Support\Helpers\Utils;

/**
 * Plugin Information Collector
 *
 * Collects plugin information using cached data for performance.
 */
class PluginCollector extends AbstractCollector
{
	/**
	 * Collect plugin information
	 *
	 * @return array
	 */
	public function collect(): array
	{
		$all_plugins = Utils::get_plugins_cached();
		$active_plugins = get_option('active_plugins', []);
		$mu_plugins = get_mu_plugins();

		$active_count = count($active_plugins);
		$inactive_count = count($all_plugins) - $active_count;

		return [
			$this->item(__('Total Plugins', 'ultimate-security'), count($all_plugins)),
			$this->item(__('Active Plugins', 'ultimate-security'), $active_count),
			$this->item(__('Inactive Plugins', 'ultimate-security'), $inactive_count),
			$this->item(__('Must-Use Plugins', 'ultimate-security'), count($mu_plugins)),
			$this->item(__('Plugin Directory', 'ultimate-security'), WP_PLUGIN_DIR),
		];
	}

	/**
	 * Get active plugins list
	 *
	 * @return array
	 */
	public function getActivePlugins(): array
	{
		$all_plugins = Utils::get_plugins_cached();
		$active_plugins = get_option('active_plugins', []);
		$active_list = [];

		foreach ($active_plugins as $plugin_path) {
			if (isset($all_plugins[$plugin_path])) {
				$plugin = $all_plugins[$plugin_path];
				$active_list[] = [
					'name' => $plugin['Name'],
					'version' => $plugin['Version'],
					'author' => $plugin['Author'],
					'path' => $plugin_path,
				];
			}
		}

		return $active_list;
	}

	/**
	 * Get inactive plugins list
	 *
	 * @return array
	 */
	public function getInactivePlugins(): array
	{
		$all_plugins = Utils::get_plugins_cached();
		$active_plugins = get_option('active_plugins', []);
		$inactive_list = [];

		foreach ($all_plugins as $plugin_path => $plugin) {
			if (!in_array($plugin_path, $active_plugins)) {
				$inactive_list[] = [
					'name' => $plugin['Name'],
					'version' => $plugin['Version'],
					'author' => $plugin['Author'],
					'path' => $plugin_path,
				];
			}
		}

		return $inactive_list;
	}

	/**
	 * Get collector name
	 *
	 * @return string
	 */
	public function getName(): string
	{
		return 'plugins';
	}

	/**
	 * Get collector label
	 *
	 * @return string
	 */
	public function getLabel(): string
	{
		return __('Plugins', 'ultimate-security');
	}
}
