<?php
/**
 * Get Current User Tool.
 *
 * Retrieve information about the currently logged-in user including ID,
 * username, email, roles, and capabilities using wp_get_current_user().
 *
 * @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;
}

/**
 * Get_Current_User_Tool class.
 */
class Get_Current_User_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Get information about the currently logged-in user including their 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(),
			'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 is_user_logged_in();
	}

	/**
	 * 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;
	}

	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- required by abstract Tool::execute()
	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param array $args The tool arguments.
	 *
	 * @return array The tool execution result.
	 * @since     1.0.0
	 */
	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Required by abstract method signature.
	protected function execute( $args ) {
		$user = wp_get_current_user();

		if ( ! $user->exists() ) {
			return $this->error_response( 'No user is currently logged in' );
		}

		$user_data = array(
			'id'           => $user->ID,
			'username'     => $user->user_login,
			'email'        => $user->user_email,
			'display_name' => $user->display_name,
			'first_name'   => $user->first_name,
			'last_name'    => $user->last_name,
			'nickname'     => $user->nickname,
			'roles'        => $user->roles,
			'registered'   => $user->user_registered,
		);

		// Include key capabilities
		$key_caps = array(
			'manage_options',
			'edit_posts',
			'edit_others_posts',
			'publish_posts',
			'edit_pages',
			'edit_others_pages',
			'publish_pages',
			'manage_categories',
			'moderate_comments',
			'upload_files',
			'edit_theme_options',
			'install_plugins',
			'activate_plugins',
			'install_themes',
			'switch_themes',
			'create_users',
			'edit_users',
			'delete_users',
			'list_users',
		);

		$capabilities = array();
		foreach ( $key_caps as $cap ) {
			if ( $user->has_cap( $cap ) ) {
				$capabilities[] = $cap;
			}
		}
		$user_data['capabilities'] = $capabilities;

		return $this->success_response(
			'Current user retrieved successfully',
			array(
				'user' => $user_data,
			)
		);
	}
}
