<?php
/**
 * List Roles Tool.
 *
 * Lists all user roles with their display names, capability counts, and
 * user counts per role.
 *
 * @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://maximum.software/
 *
 * 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' ) ) {
	return;
}

/**
 * List_Roles_Tool class.
 */
class List_Roles_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'List all user roles with their display names and capability counts.';
	}

	/**
	 * 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(
				'limit'  => array(
					'type'        => 'integer',
					'description' => 'Maximum number of roles to return',
					'default'     => 100,
				),
				'offset' => array(
					'type'        => 'integer',
					'description' => 'Number of roles to skip',
					'default'     => 0,
				),
			),
			'required'   => array(),
		);
	}

	/**
	 * 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 ) {
		$wp_roles  = wp_roles();
		$all_roles = array();

		foreach ( $wp_roles->roles as $role_slug => $role_data ) {
			$role_entry = array(
				'slug'             => $role_slug,
				'name'             => $role_data['name'],
				'capability_count' => count( array_filter( $role_data['capabilities'] ) ),
			);

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

			$all_roles[] = $role_entry;
		}

		$limit  = isset( $args['limit'] ) ? intval( $args['limit'] ) : 100;
		$offset = isset( $args['offset'] ) ? intval( $args['offset'] ) : 0;

		// Apply pagination
		$total_roles = count( $all_roles );
		$roles       = array_slice( $all_roles, $offset, $limit );
		$has_more    = ( $offset + count( $roles ) ) < $total_roles;

		return $this->success_response(
			'Roles retrieved successfully',
			array(
				'roles'    => $roles,
				'count'    => count( $roles ),
				'total'    => $total_roles,
				'limit'    => $limit,
				'offset'   => $offset,
				'has_more' => $has_more,
			)
		);
	}
}
