<?php
/**
 * `wp blaminhor classic-editor ...` — Classic Editor & Widgets commands.
 *
 * @package BlaminhorEssentials
 */

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

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

/**
 * Toggle the classic editor / widgets and related block-style cleanup flags.
 */
class Blaminhor_Essentials_CLI_Classic_Editor extends Blaminhor_Essentials_CLI_Module {

	/**
	 * @var string
	 */
	protected $module_id = 'classic-editor';

	/**
	 * Show the current Classic Editor settings.
	 *
	 * ## OPTIONS
	 *
	 * [--format=<format>]
	 * : Render format (table, json, csv, yaml). Default: table.
	 *
	 * @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' => 'enable_classic_editor',     'value' => $settings['enable_classic_editor'] ? 'true' : 'false' ),
			array( 'key' => 'enable_classic_widgets',    'value' => $settings['enable_classic_widgets'] ? 'true' : 'false' ),
			array( 'key' => 'remove_block_styles',       'value' => $settings['remove_block_styles'] ? 'true' : 'false' ),
			array( 'key' => 'remove_admin_block_styles', 'value' => $settings['remove_admin_block_styles'] ? 'true' : 'false' ),
		);

		$this->format_for_output( $rows, $assoc_args, array( 'key', 'value' ) );
	}

	/**
	 * Update Classic Editor settings.
	 *
	 * ## OPTIONS
	 *
	 * [--enable-classic-editor=<bool>]
	 * : Restore the TinyMCE classic editor (disables Gutenberg).
	 *
	 * [--enable-classic-widgets=<bool>]
	 * : Restore the classic widgets screen.
	 *
	 * [--remove-block-styles=<bool>]
	 * : Remove core block CSS from the front-end.
	 *
	 * [--remove-admin-block-styles=<bool>]
	 * : Remove core block CSS from the admin screens.
	 *
	 * ## EXAMPLES
	 *
	 *     $ wp blaminhor classic-editor set --enable-classic-editor=true --enable-classic-widgets=true
	 *
	 * @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();
		$module  = $this->get_module_instance();
		$current = $module->get_effective_settings();

		$bool_map = array(
			'enable-classic-editor'     => 'enable_classic_editor',
			'enable-classic-widgets'    => 'enable_classic_widgets',
			'remove-block-styles'       => 'remove_block_styles',
			'remove-admin-block-styles' => 'remove_admin_block_styles',
		);

		$data = array();
		foreach ( $bool_map as $flag => $key ) {
			if ( array_key_exists( $flag, $assoc_args ) ) {
				if ( $this->parse_bool( $assoc_args[ $flag ] ) ) {
					$data[ $key ] = '1';
				}
			} elseif ( ! empty( $current[ $key ] ) ) {
				$data[ $key ] = '1';
			}
		}

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

		blaminhor_essentials_log( 'Classic Editor settings updated via WP-CLI.', 'info', 'cli', array( 'event' => 'classic_editor_set' ) );
		WP_CLI::success( __( 'Classic Editor settings updated.', 'blaminhor-essentials' ) );
	}
}
