<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * CLI Command to recover random slug
 *
 * @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 HappyFenceNGWPCLILogin 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 show_login_slug( $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;
			}

			$slug = HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PCORE->custom_login_slug, HappyFenceNGPlaceholder::EMPTY_STRING );

			if ( ! empty( $slug ) ) {
				WP_CLI::success( 'Current login slug: /' . $slug );
			} else {
				WP_CLI::warning( 'No custom or generated login slug is currently set.' );
			}
		}
	}

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