<?php
/**
 * `wp powersuite status` command.
 *
 * @package WP_PowerSuite\CLI
 * @since 1.0.7
 */

namespace WP_PowerSuite\CLI;

use WP_PowerSuite\CLI\Support\Cli;
use WP_PowerSuite\Core\ModuleRegistry;
use WP_PowerSuite\Core\NetworkScope;
use WP_PowerSuite\Core\OptionStore;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Show a high-level status overview of the WP PowerSuite installation.
 *
 * @since 1.0.7
 */
final class StatusCommand {

	/**
	 * Display WP PowerSuite status.
	 *
	 * Reports plugin/WordPress/PHP versions, multisite and network-activation
	 * state, module counts, license state, environment type, and debug/CLI info.
	 * No secrets (license keys, API tokens, passwords) are ever printed.
	 *
	 * ## OPTIONS
	 *
	 * [--format=<format>]
	 * : Render output in a particular format.
	 * ---
	 * default: table
	 * options:
	 *   - table
	 *   - json
	 *   - yaml
	 * ---
	 *
	 * ## EXAMPLES
	 *
	 *     # Human-readable overview
	 *     $ wp powersuite status
	 *
	 *     # Machine-readable, for automation
	 *     $ wp powersuite status --format=json
	 *
	 * @when after_wp_load
	 *
	 * @param array<int, string>    $args       Positional arguments (unused).
	 * @param array<string, mixed>  $assoc_args Associative arguments.
	 * @return void
	 */
	public function __invoke( array $args, array $assoc_args ): void {
		$registry = Cli::registry();
		$all      = $registry->getAllModules();
		$enabled  = OptionStore::getInstance()->getEnabledModules();

		global $wp_version;

		$data = [
			'powersuite_version' => defined( 'WP_POWERSUITE_VERSION' ) ? WP_POWERSUITE_VERSION : 'unknown',
			'wordpress_version'  => is_string( $wp_version ) ? $wp_version : (string) get_bloginfo( 'version' ),
			'php_version'        => PHP_VERSION,
			'multisite'          => is_multisite(),
			'network_activated'  => NetworkScope::isNetworkActivated(),
			'settings_scope'     => NetworkScope::usesNetworkStorage() ? 'network' : 'site',
			'modules_total'      => count( $all ),
			'modules_enabled'    => count( array_intersect( array_keys( $all ), $enabled ) ),
			'license_state'      => Cli::licenseState(),
			'environment'        => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'unknown',
			'wp_debug'           => defined( 'WP_DEBUG' ) && WP_DEBUG,
			'wp_cli_version'     => defined( 'WP_CLI_VERSION' ) ? WP_CLI_VERSION : 'unknown',
		];

		Cli::renderKeyValue( $data, $assoc_args );
	}
}
