<?php
/**
 * Get Role Details Tool.
 *
 * Gets detailed information about a specific role including all its capabilities
 * and user count.
 *
 * @package   Agenistrator
 * @since     1.0.0
 * @author    Alex Ruban <alex@maximum.guru>
 * @author    Maximum Software Inc <contact@maximum.software>
 * @license   GPL-3.0-only
 * @link      https://agenistrator.com/
 *
 * SPDX-FileCopyrightText: 2025-2026 Alex Ruban <alex@maximum.guru>
 * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc <contact@maximum.software>
 * SPDX-License-Identifier: GPL-3.0-only
 */

namespace Agenistrator;

// Direct access prevention
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Get_Role_Details_Tool class.
 */
class Get_Role_Details_Tool extends Tool {

	/**
	 * Get the tool ID.
	 *
	 * @return string The tool ID.
	 * @since     1.0.0
	 */
	public function get_id() {
		return 'get_role_details';
	}

	/**
	 * Get the tool name.
	 *
	 * @return string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return __( 'Get Role Details', 'agenistrator' );
	}

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Get detailed information about a specific role including all its capabilities.';
	}

	/**
	 * Get the parameters schema for this tool.
	 *
	 * @return array Parameters schema in JSON Schema format.
	 * @since     1.0.0
	 */
	public function get_parameters() {
		return array(
			'type'       => 'object',
			'properties' => array(
				'role' => array(
					'type'        => 'string',
					'description' => 'The role slug (e.g., "editor", "author", "subscriber")',
				),
			),
			'required'   => array( 'role' ),
		);
	}

	/**
	 * Check if the current user can execute this tool.
	 *
	 * @return bool True if the current user has permission.
	 * @since     1.0.0
	 */
	public function current_user_can_execute() {
		return current_user_can( 'list_users' );
	}

	/**
	 * Check if this tool is read-only.
	 *
	 * @return bool True if the tool only reads data.
	 * @since     1.0.0
	 */
	public function is_read_only() {
		return true;
	}

	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param array $args The tool arguments.
	 *
	 * @return array The tool execution result.
	 * @since     1.0.0
	 */
	protected function execute( $args ) {
		$role_slug = sanitize_key( $args['role'] );
		$wp_roles  = wp_roles();

		if ( ! isset( $wp_roles->roles[ $role_slug ] ) ) {
			return $this->error_response( 'Role not found: ' . $role_slug );
		}

		$role_data    = $wp_roles->roles[ $role_slug ];
		$capabilities = array_keys( array_filter( $role_data['capabilities'] ) );
		sort( $capabilities );

		$is_builtin = in_array( $role_slug, array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ), true );

		$result = array(
			'slug'             => $role_slug,
			'name'             => $role_data['name'],
			'is_builtin'       => $is_builtin,
			'capabilities'     => $capabilities,
			'capability_count' => count( $capabilities ),
		);

		$translated_name = translate_user_role( $role_data['name'] );
		if ( $translated_name !== $role_data['name'] ) {
			$result['translated_name'] = $translated_name;
		}

		return $this->success_response( 'Role details retrieved successfully', $result );
	}
}
