<?php
/**
 * Plugin and theme collector.
 *
 * @package MonitorCat\Modules\Diagnostics\Collector
 */

declare(strict_types=1);

namespace MonitorCat\Modules\Diagnostics\Collector;

defined( 'ABSPATH' ) || exit;

/**
 * Collects information about the active theme, must-use plugins, and installed plugins.
 */
final class PluginThemeCollector {

	/**
	 * Collect plugin and theme data.
	 *
	 * Returns flat items with status for health scoring, plus a structured
	 * plugins array for detailed rendering in the Plugins & Theme tab.
	 *
	 * @return array{label: string, icon: string, items: array<int, array{label: string, value: string, status?: string}>, plugins: array<string, mixed>}
	 */
	public function collect(): array {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$all_plugins = get_plugins();
		$items       = array();

		// Theme info (flat items).
		$this->add_theme_items( $items );

		// Single pass: build detailed plugin lists and count active/inactive.
		$plugins = $this->get_plugins_data( $all_plugins );

		// Plugin summary items with status for health scoring (uses counts from above).
		$this->add_plugin_summary_items( $items, $plugins['counts'] );

		return array(
			'label'   => __( 'Plugins & Theme', 'monitor-cat' ),
			'icon'    => 'fa-puzzle-piece',
			'items'   => $items,
			'plugins' => $plugins,
		);
	}

	/**
	 * Add active theme information as flat items.
	 *
	 * @param array<int, array<string, mixed>> $items Reference to the items array.
	 */
	private function add_theme_items( array &$items ): void {
		$theme = wp_get_theme();

		$items[] = array(
			'label' => __( 'Active Theme', 'monitor-cat' ),
			'value' => $theme->get( 'Name' ) . ' v' . $theme->get( 'Version' ),
		);

		$items[] = array(
			'label' => __( 'Theme Author', 'monitor-cat' ),
			'value' => $theme->get( 'Author' ),
		);

		if ( $theme->parent() ) {
			$items[] = array(
				'label' => __( 'Parent Theme', 'monitor-cat' ),
				'value' => $theme->parent()->get( 'Name' ) . ' v' . $theme->parent()->get( 'Version' ),
			);
		}
	}

	/**
	 * Add plugin count summary items with health status.
	 *
	 * @param array<int, array<string, mixed>>                        $items  Reference to the items array.
	 * @param array{active: int, inactive: int, mu: int, total: int} $counts Pre-computed plugin counts.
	 */
	private function add_plugin_summary_items( array &$items, array $counts ): void {
		$items[] = array(
			'label'  => __( 'Active Plugins', 'monitor-cat' ),
			'value'  => (string) $counts['active'],
			'status' => $counts['active'] > 30 ? 'warning' : 'good',
		);

		if ( $counts['inactive'] > 0 ) {
			$items[] = array(
				'label'  => __( 'Inactive Plugins', 'monitor-cat' ),
				'value'  => sprintf(
					/* translators: %d: number of inactive plugins */
					_n( '%d inactive plugin', '%d inactive plugins', $counts['inactive'], 'monitor-cat' ),
					$counts['inactive']
				),
				'status' => 'warning',
			);
		} else {
			$items[] = array(
				'label'  => __( 'Inactive Plugins', 'monitor-cat' ),
				'value'  => __( 'None', 'monitor-cat' ),
				'status' => 'good',
			);
		}

		$items[] = array(
			'label' => __( 'Total Plugins', 'monitor-cat' ),
			'value' => (string) $counts['total'],
		);
	}

	/**
	 * Get structured plugin data for detailed rendering.
	 *
	 * Single pass over all plugins — also produces counts reused by summary items.
	 *
	 * @param array<string, array<string, mixed>> $all_plugins All installed plugins.
	 * @return array{active: array<int, array<string, string>>, inactive: array<int, array<string, string>>, mu: array<int, array<string, string>>, counts: array<string, int>}
	 */
	private function get_plugins_data( array $all_plugins ): array {
		$active   = array();
		$inactive = array();

		foreach ( $all_plugins as $plugin_file => $plugin_data ) {
			$entry = array(
				'name'    => $plugin_data['Name'],
				'version' => $plugin_data['Version'],
				'author'  => wp_strip_all_tags( $plugin_data['Author'] ),
				'file'    => $plugin_file,
			);

			if ( is_plugin_active( $plugin_file ) ) {
				$active[] = $entry;
			} else {
				$inactive[] = $entry;
			}
		}

		$mu_plugins = get_mu_plugins();
		$mu         = array();

		foreach ( $mu_plugins as $plugin_data ) {
			$mu[] = array(
				'name'    => $plugin_data['Name'],
				'version' => $plugin_data['Version'] ? $plugin_data['Version'] : __( 'N/A', 'monitor-cat' ),
				'author'  => wp_strip_all_tags( $plugin_data['Author'] ),
			);
		}

		return array(
			'active'   => $active,
			'inactive' => $inactive,
			'mu'       => $mu,
			'counts'   => array(
				'active'   => count( $active ),
				'inactive' => count( $inactive ),
				'mu'       => count( $mu ),
				'total'    => count( $all_plugins ),
			),
		);
	}
}
