<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * CLI Command to view runtime module dashboard
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	class HappyFenceNGCLIDashboard extends HappyFenceNGRuntimeExceptions {

		/**PHPCS -> code compliance: we put useful comments where needed..
		 *
		 * @param string $args ...are arguments.
		 * @param string $assoc_args ..uhm maybe an array of arguments?.
		 */
		public function dashboard( $args, $assoc_args ) {

			$input_pass_raw  = $assoc_args['cli-pass'] ?? null;
			$expected_length = 35;

			$input_pass = HappyFenceNGCleaner::sanitize_cli_password( $input_pass_raw, $expected_length );

			if ( is_null( $input_pass ) ) {
				WP_CLI::error( 'Invalid CLI password format or length.' );
				return;
			}

			$stored_pass = HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PCORE->wpcli_secpass, HappyFenceNGUtils::generateStrongPassword( HappyFenceNGPcoreProps::_PWD_LEN ) );

			if ( ! hash_equals( $stored_pass, $input_pass ) ) {
				WP_CLI::error( 'Invalid CLI password.' );
				return;
			}

			$functions = HappyFenceNGCStore::getFunctions();

			if ( empty( $functions ) ) {
				WP_CLI::warning( 'No modules registered or available.' );
				return;
			}

			$table = HappyFenceNGPlaceholder::EMPTY_ARRAY;
			foreach ( $functions as $module => $status ) {
				$table[] = array(
					'Module' => $module,
					'Status' => $status,
				);
			}

			$format = $assoc_args['format'] ?? 'table';
			WP_CLI\Utils\format_items( $format, $table, array( 'Module', 'Status' ) );
		}
	}

	WP_CLI::add_command( 'happyfenceng', 'HappyFenceNGCLIDashboard' );
}
