<?php
/**
 * PHP environment collector.
 *
 * @package MonitorCat\Modules\Diagnostics\Collector
 */

declare(strict_types=1);

namespace MonitorCat\Modules\Diagnostics\Collector;

defined( 'ABSPATH' ) || exit;

/**
 * Collects PHP environment information including version, extensions, and configuration.
 */
final class PhpCollector {

	/**
	 * Collect PHP environment data.
	 *
	 * @return array{label: string, icon: string, items: array<int, array{label: string, value: string, status?: string}>}
	 */
	public function collect(): array {
		return array(
			'label' => __( 'PHP', 'monitor-cat' ),
			'icon'  => 'fa-code',
			'items' => array(
				array(
					'label'  => __( 'Version', 'monitor-cat' ),
					'value'  => PHP_VERSION,
					'status' => $this->get_version_status(),
				),
				array(
					'label' => __( 'SAPI', 'monitor-cat' ),
					'value' => php_sapi_name() ? php_sapi_name() : __( 'Unknown', 'monitor-cat' ),
				),
				array(
					'label'  => __( 'Memory Limit', 'monitor-cat' ),
					'value'  => ini_get( 'memory_limit' ) ? ini_get( 'memory_limit' ) : __( 'Unknown', 'monitor-cat' ),
					'status' => $this->get_memory_limit_status(),
				),
				array(
					'label' => __( 'Max Execution Time', 'monitor-cat' ),
					'value' => ini_get( 'max_execution_time' ) . 's',
				),
				array(
					'label' => __( 'Max Input Vars', 'monitor-cat' ),
					'value' => ini_get( 'max_input_vars' ) ? ini_get( 'max_input_vars' ) : __( 'Unknown', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Upload Max Size', 'monitor-cat' ),
					'value' => ini_get( 'upload_max_filesize' ) ? ini_get( 'upload_max_filesize' ) : __( 'Unknown', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Post Max Size', 'monitor-cat' ),
					'value' => ini_get( 'post_max_size' ) ? ini_get( 'post_max_size' ) : __( 'Unknown', 'monitor-cat' ),
				),
				array(
					'label'  => __( 'cURL', 'monitor-cat' ),
					'value'  => extension_loaded( 'curl' )
						? __( 'Enabled', 'monitor-cat' )
						: __( 'Disabled', 'monitor-cat' ),
					'status' => extension_loaded( 'curl' ) ? 'good' : 'critical',
				),
				array(
					'label' => __( 'OpenSSL', 'monitor-cat' ),
					'value' => extension_loaded( 'openssl' )
						? OPENSSL_VERSION_TEXT
						: __( 'Not available', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Multibyte String', 'monitor-cat' ),
					'value' => extension_loaded( 'mbstring' )
						? __( 'Yes', 'monitor-cat' )
						: __( 'No', 'monitor-cat' ),
				),
				array(
					'label' => __( 'XML', 'monitor-cat' ),
					'value' => extension_loaded( 'xml' )
						? __( 'Yes', 'monitor-cat' )
						: __( 'No', 'monitor-cat' ),
				),
				array(
					'label' => __( 'GD Library', 'monitor-cat' ),
					'value' => extension_loaded( 'gd' )
						? __( 'Yes', 'monitor-cat' )
						: __( 'No', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Imagick', 'monitor-cat' ),
					'value' => extension_loaded( 'imagick' )
						? __( 'Yes', 'monitor-cat' )
						: __( 'No', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Intl', 'monitor-cat' ),
					'value' => extension_loaded( 'intl' )
						? __( 'Yes', 'monitor-cat' )
						: __( 'No', 'monitor-cat' ),
				),
				array(
					'label' => __( 'Zlib', 'monitor-cat' ),
					'value' => extension_loaded( 'zlib' )
						? __( 'Yes', 'monitor-cat' )
						: __( 'No', 'monitor-cat' ),
				),
			),
		);
	}

	/**
	 * Get the status for the PHP version.
	 *
	 * @return string
	 */
	private function get_version_status(): string {
		if ( version_compare( PHP_VERSION, '8.2', '>=' ) ) {
			return 'good';
		}

		if ( version_compare( PHP_VERSION, '8.0', '>=' ) ) {
			return 'warning';
		}

		return 'critical';
	}

	/**
	 * Get the status for the memory limit.
	 *
	 * @return string
	 */
	private function get_memory_limit_status(): string {
		$memory_limit = (int) ini_get( 'memory_limit' );

		return $memory_limit >= 256 ? 'good' : 'warning';
	}
}
