<?php
/**
 * `wp blaminhor ure ...` — User Role Editor commands (scope: read-only).
 *
 * Mutating roles via CLI is already covered by `wp role` and `wp cap` in
 * core WP-CLI. We expose a read-only view that integrates the module's
 * custom capability groups + the user counts admins typically need.
 *
 * @package BlaminhorEssentials
 */

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

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

class Blaminhor_Essentials_CLI_User_Role_Editor extends Blaminhor_Essentials_CLI_Module {

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

	/**
	 * List every WordPress role with its user count.
	 *
	 * ## OPTIONS
	 *
	 * [--format=<format>]
	 * : Render format (table, json, csv, yaml, count, ids). Default: table.
	 *
	 * @subcommand list-roles
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args (unused).
	 * @param array $assoc_args Associative args.
	 */
	public function list_roles( $args, $assoc_args ) {
		if ( ! function_exists( 'wp_roles' ) ) {
			WP_CLI::error( __( 'WordPress roles API not available.', 'blaminhor-essentials' ) );
		}
		$wp_roles  = wp_roles();
		$counts    = count_users();
		$by_role   = isset( $counts['avail_roles'] ) ? (array) $counts['avail_roles'] : array();

		$rows = array();
		foreach ( $wp_roles->role_objects as $slug => $role ) {
			$rows[] = array(
				'slug'        => (string) $slug,
				'name'        => translate_user_role( $wp_roles->role_names[ $slug ] ),
				'capabilities'=> (string) count( (array) $role->capabilities ),
				'users'       => (string) (int) ( isset( $by_role[ $slug ] ) ? $by_role[ $slug ] : 0 ),
			);
		}
		$this->format_for_output( $rows, $assoc_args, array( 'slug', 'name', 'capabilities', 'users' ) );
	}

	/**
	 * Show the capabilities map of a single role.
	 *
	 * ## OPTIONS
	 *
	 * <slug>
	 * : Role slug (e.g. administrator, editor, my-custom-role).
	 *
	 * [--granted-only]
	 * : Only print the capabilities that evaluate to true.
	 *
	 * [--format=<format>]
	 * : Render format. Default: table.
	 *
	 * @subcommand get-role
	 * @when after_wp_load
	 *
	 * @param array $args       Positional args: [<slug>].
	 * @param array $assoc_args Associative args.
	 */
	public function get_role( $args, $assoc_args ) {
		if ( empty( $args[0] ) ) {
			WP_CLI::error( __( 'Missing <slug> argument.', 'blaminhor-essentials' ) );
		}
		$slug = sanitize_key( (string) $args[0] );
		$role = get_role( $slug );
		if ( ! $role ) {
			WP_CLI::error( sprintf(
				/* translators: %s: role slug */
				__( 'No role with slug "%s".', 'blaminhor-essentials' ),
				$slug
			) );
		}

		$only_granted = isset( $assoc_args['granted-only'] );
		$rows         = array();
		foreach ( (array) $role->capabilities as $cap => $granted ) {
			if ( $only_granted && ! $granted ) {
				continue;
			}
			$rows[] = array(
				'capability' => (string) $cap,
				'granted'    => $granted ? 'true' : 'false',
			);
		}
		ksort( $rows );
		$this->format_for_output( $rows, $assoc_args, array( 'capability', 'granted' ) );
	}

	/**
	 * Show the User Role Editor module settings (read-only).
	 *
	 * @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' => 'show_deprecated_caps',  'value' => $settings['show_deprecated_caps'] ? 'true' : 'false' ),
			array( 'key' => 'allow_admin_role_edit', 'value' => $settings['allow_admin_role_edit'] ? 'true' : 'false' ),
			array( 'key' => 'label_display_mode',    'value' => (string) $settings['label_display_mode'] ),
			array( 'key' => 'custom_capabilities',   'value' => implode( ',', (array) $settings['custom_capabilities'] ) ),
		);
		$this->format_for_output( $rows, $assoc_args, array( 'key', 'value' ) );
	}
}
