<?php
/**
 * Get User Details Tool.
 *
 * Retrieve detailed information about a specific user by ID, login, or email
 * including profile fields, roles, capabilities, and registration date.
 *
 * @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_User_Details_Tool class.
 */
class Get_User_Details_Tool extends Tool {

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

	/**
	 * Get the tool name.
	 *
	 * @return string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return __( 'Get User 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 user by ID, username, or email.';
	}

	/**
	 * 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(
				'id'       => array(
					'type'        => 'integer',
					'description' => 'The user ID',
				),
				'username' => array(
					'type'        => 'string',
					'description' => 'The username (login)',
				),
				'email'    => array(
					'type'        => 'string',
					'description' => 'The user email address',
				),
			),
			'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 ) {
		$user = null;

		if ( isset( $args['id'] ) && ! empty( $args['id'] ) ) {
			$user = get_user_by( 'ID', intval( $args['id'] ) );
		} elseif ( isset( $args['username'] ) && ! empty( $args['username'] ) ) {
			$user = get_user_by( 'login', sanitize_user( $args['username'] ) );
		} elseif ( isset( $args['email'] ) && ! empty( $args['email'] ) ) {
			$user = get_user_by( 'email', sanitize_email( $args['email'] ) );
		} else {
			return $this->error_response( 'Please provide id, username, or email' );
		}

		if ( ! $user ) {
			return $this->error_response( 'User not found' );
		}

		$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,
			'description'  => $user->description,
			'url'          => $user->user_url,
			'roles'        => $user->roles,
			'registered'   => $user->user_registered,
		);

		// Get post counts
		$post_count              = count_user_posts( $user->ID, 'post', true );
		$user_data['post_count'] = $post_count;

		// Get recent posts
		$recent_posts = get_posts(
			array(
				'author'         => $user->ID,
				'posts_per_page' => 5,
				'post_status'    => 'any',
			)
		);

		if ( ! empty( $recent_posts ) ) {
			$user_data['recent_posts'] = array();
			foreach ( $recent_posts as $post ) {
				$user_data['recent_posts'][] = array(
					'id'     => $post->ID,
					'title'  => $post->post_title,
					'status' => $post->post_status,
					'date'   => $post->post_date,
				);
			}
		}

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