<?php
/**
 * `wp blaminhor hide-login ...` — Hide Login module commands.
 *
 * @package BlaminhorEssentials
 */

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

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
	return;
}

/**
 * Change the WordPress login URL and block wp-login.php from the CLI.
 *
 * Misconfiguring the login slug locks you out of the admin. Double-check
 * the slug you set; the CLI does not enforce a recovery mechanism.
 */
class Blaminhor_Essentials_CLI_Hide_Login extends Blaminhor_Essentials_CLI_Module {

	/**
	 * @var string
	 */
	protected $module_id = 'hide-login';

	/**
	 * Show the current Hide Login settings.
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args.
	 */
	public function get( $args, $assoc_args ) {
		$module   = $this->get_module_instance();
		$settings = $module->get_effective_settings();

		$rows = array(
			array( 'key' => 'enabled',       'value' => $settings['enabled'] ? 'true' : 'false' ),
			array( 'key' => 'login_slug',    'value' => (string) $settings['login_slug'] ),
			array( 'key' => 'redirect_slug', 'value' => (string) $settings['redirect_slug'] ),
			array( 'key' => 'effective_login_url', 'value' => home_url( '/' . ltrim( (string) $settings['login_slug'], '/' ) . '/' ) ),
		);
		$this->format_for_output( $rows, $assoc_args, array( 'key', 'value' ) );
	}

	/**
	 * Print whether hide-login is currently enabled (porcelain).
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args (unused).
	 */
	public function status( $args, $assoc_args ) {
		$module = $this->get_module_instance();
		if ( $module->get_setting( 'enabled' ) ) {
			WP_CLI::line( 'enabled' );
			return;
		}
		WP_CLI::line( 'disabled' );
		WP_CLI::halt( 1 );
	}

	/**
	 * Enable the Hide Login feature.
	 *
	 * ## OPTIONS
	 *
	 * [--slug=<slug>]
	 * : New login slug. Defaults to keeping the current value.
	 *
	 * [--redirect=<slug>]
	 * : Where to redirect anonymous hits to wp-login.php. Default: keep current.
	 *
	 * ## EXAMPLES
	 *
	 *     $ wp blaminhor hide-login enable --slug=back-door
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args.
	 */
	public function enable( $args, $assoc_args ) {
		$this->ensure_capabilities();
		$assoc_args['enabled'] = '1';
		$this->apply( $assoc_args, __( 'Hide Login enabled.', 'blaminhor-essentials' ), 'hide_login_enable' );
	}

	/**
	 * Disable Hide Login (wp-login.php becomes reachable again).
	 *
	 * ## OPTIONS
	 *
	 * [--yes]
	 * : Skip the interactive confirmation prompt.
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args.
	 */
	public function disable( $args, $assoc_args ) {
		$this->ensure_capabilities();
		$module = $this->get_module_instance();

		if ( ! $module->get_setting( 'enabled' ) ) {
			WP_CLI::warning( __( 'Hide Login is already disabled.', 'blaminhor-essentials' ) );
			return;
		}

		WP_CLI::confirm(
			__( 'Disable Hide Login? wp-login.php will be reachable again on the standard URL.', 'blaminhor-essentials' ),
			$assoc_args
		);

		$assoc_args['__no_enabled__'] = true;
		$this->apply( $assoc_args, __( 'Hide Login disabled.', 'blaminhor-essentials' ), 'hide_login_disable' );
	}

	/**
	 * Update Hide Login settings without toggling on/off.
	 *
	 * ## OPTIONS
	 *
	 * [--slug=<slug>]
	 * [--redirect=<slug>]
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args.
	 */
	public function set( $args, $assoc_args ) {
		$this->ensure_capabilities();
		$this->apply( $assoc_args, __( 'Hide Login settings updated.', 'blaminhor-essentials' ), 'hide_login_set' );
	}

	/**
	 * Build the $data payload for process_settings() while preserving the
	 * fields that weren't explicitly overridden.
	 */
	private function apply( $assoc_args, $success, $event ) {
		$module  = $this->get_module_instance();
		$current = $module->get_effective_settings();

		$data = array(
			'login_slug'    => isset( $assoc_args['slug'] ) ? (string) $assoc_args['slug'] : (string) $current['login_slug'],
			'redirect_slug' => isset( $assoc_args['redirect'] ) ? (string) $assoc_args['redirect'] : (string) $current['redirect_slug'],
		);

		if ( empty( $assoc_args['__no_enabled__'] ) ) {
			if ( isset( $assoc_args['enabled'] ) ) {
				if ( $this->parse_bool( $assoc_args['enabled'] ) ) {
					$data['enabled'] = '1';
				}
			} elseif ( ! empty( $current['enabled'] ) ) {
				$data['enabled'] = '1';
			}
		}

		$result = $module->apply_settings( $data );
		if ( is_wp_error( $result ) ) {
			WP_CLI::error( $result->get_error_message() );
		}

		blaminhor_essentials_log( sprintf( 'Hide Login updated via WP-CLI (event=%s).', $event ), 'info', 'cli', array( 'event' => $event ) );
		WP_CLI::success( $success );
	}
}
