<?php
/**
 * `wp blaminhor mute-emails ...` — Mute Core Emails commands.
 *
 * @package BlaminhorEssentials
 */

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

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

/**
 * Mute or unmute specific WordPress core notification emails.
 */
class Blaminhor_Essentials_CLI_Mute_Core_Emails extends Blaminhor_Essentials_CLI_Module {

	/**
	 * @var string
	 */
	protected $module_id = 'mute-core-emails';

	/**
	 * The list of email keys the module knows about — kept here so the CLI
	 * can validate user input without instantiating the module.
	 *
	 * @return array<string>
	 */
	private function known_keys() {
		return array(
			'disable_auto_core_update_email',
			'disable_auto_plugin_update_email',
			'disable_auto_theme_update_email',
			'disable_core_update_notification',
			'disable_new_user_admin_email',
			'disable_new_user_email',
			'disable_password_change_email',
			'disable_password_change_admin_email',
			'disable_email_change_email',
			'disable_comment_notification',
			'disable_comment_moderation',
			'disable_recovery_mode_email',
		);
	}

	/**
	 * Show which core notification emails are muted.
	 *
	 * ## 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();
		foreach ( $this->known_keys() as $key ) {
			$rows[] = array(
				'email' => $key,
				'muted' => ! empty( $settings[ $key ] ) ? 'true' : 'false',
			);
		}
		$this->format_for_output( $rows, $assoc_args, array( 'email', 'muted' ) );
	}

	/**
	 * Mute a specific WordPress core email.
	 *
	 * ## OPTIONS
	 *
	 * <email>
	 * : Email key to mute (see `wp blaminhor mute-emails get` for the list).
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args: [<email>].
	 * @param array $assoc_args Associative args (unused).
	 */
	public function mute( $args, $assoc_args ) {
		$this->ensure_capabilities();
		$this->toggle( $args, true );
	}

	/**
	 * Unmute a specific WordPress core email.
	 *
	 * ## OPTIONS
	 *
	 * <email>
	 * : Email key to unmute.
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args: [<email>].
	 * @param array $assoc_args Associative args (unused).
	 */
	public function unmute( $args, $assoc_args ) {
		$this->ensure_capabilities();
		$this->toggle( $args, false );
	}

	/**
	 * Shared mute/unmute helper.
	 *
	 * @param array $args      Positional args.
	 * @param bool  $should_mute true to mute the key, false to unmute.
	 */
	private function toggle( $args, $should_mute ) {
		$module = $this->get_module_instance();

		if ( empty( $args[0] ) ) {
			WP_CLI::error( __( 'Missing email key. Run `wp blaminhor mute-emails get` to list available keys.', 'blaminhor-essentials' ) );
		}
		$key = sanitize_key( (string) $args[0] );
		if ( ! in_array( $key, $this->known_keys(), true ) ) {
			WP_CLI::error( sprintf(
				/* translators: %s: unknown email key */
				__( 'Unknown email key "%s".', 'blaminhor-essentials' ),
				$key
			) );
		}

		$current = $module->get_effective_settings();
		$data    = array();
		foreach ( $this->known_keys() as $known ) {
			if ( $known === $key ) {
				if ( $should_mute ) {
					$data[ $known ] = '1';
				}
			} elseif ( ! empty( $current[ $known ] ) ) {
				$data[ $known ] = '1';
			}
		}

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

		$event = $should_mute ? 'mute_email' : 'unmute_email';
		blaminhor_essentials_log( sprintf( 'Mute Core Emails: %s "%s" via WP-CLI.', $event, $key ), 'info', 'cli', array( 'event' => $event ) );
		WP_CLI::success( sprintf(
			$should_mute
				/* translators: %s: email key */
				? __( 'Email "%s" muted.', 'blaminhor-essentials' )
				/* translators: %s: email key */
				: __( 'Email "%s" unmuted.', 'blaminhor-essentials' ),
			$key
		) );
	}
}
