<?php
/**
 * Delete User Tool.
 *
 * Delete WordPress users with optional content reassignment to another user.
 * Prevents self-deletion and deleting the last administrator.
 *
 * @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;
}

/**
 * Delete_User_Tool class.
 */
class Delete_User_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Delete a user account with option to reassign their content.';
	}

	/**
	 * 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 to delete',
				),
				'reassign_to' => array(
					'type'        => 'integer',
					'description' => 'User ID to reassign posts to (if omitted, content will be deleted)',
				),
			),
			'required'   => array( 'id' ),
		);
	}

	/**
	 * 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( 'delete_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 false;
	}

	/**
	 * 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 ) {
		if ( ! isset( $args['id'] ) || empty( $args['id'] ) ) {
			return $this->error_response( 'User ID is required' );
		}

		$user_id = intval( $args['id'] );
		$user    = get_user_by( 'ID', $user_id );

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

		// Check if current user can delete this user
		if ( ! WordPress_Utils::can_delete_user( $user_id ) ) {
			return $this->error_response( 'You do not have permission to delete this user' );
		}

		$username = $user->user_login;
		$email    = $user->user_email;

		$reassign = isset( $args['reassign_to'] ) ? intval( $args['reassign_to'] ) : null;

		if ( $reassign ) {
			$reassign_user = get_user_by( 'ID', $reassign );
			if ( ! $reassign_user ) {
				return $this->error_response( 'Reassign user not found' );
			}
		}

		require_once ABSPATH . 'wp-admin/includes/user.php';

		$deleted = wp_delete_user( $user_id, $reassign );

		if ( ! $deleted ) {
			return $this->error_response( 'Failed to delete user' );
		}

		$response_data = array(
			'user_id'  => $user_id,
			'username' => $username,
			'email'    => $email,
		);

		if ( $reassign ) {
			$response_data['content_reassigned_to'] = $reassign;
		} else {
			$response_data['content_deleted'] = true;
		}

		return $this->success_response( 'User deleted successfully', $response_data );
	}
}
