<?php
/**
 * Uninstall Plugin Tool.
 *
 * Allows the agent to uninstall plugins. Deactivates first if active, runs
 * uninstall hooks, and deletes plugin files.
 *
 * @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;
}

/**
 * Uninstall_Plugin_Tool class.
 */
class Uninstall_Plugin_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Uninstall a plugin by removing its files.';
	}

	/**
	 * 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(
				'plugin' => array(
					'type'        => 'string',
					'description' => 'Plugin file path (e.g., "akismet/akismet.php"), slug (e.g., "akismet"), or plugin name',
				),
			),
			'required'   => array( 'plugin' ),
		);
	}

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

	/**
	 * 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 ) {
		// Check if file modifications are disabled
		if ( ! wp_is_file_mod_allowed( 'plugin_install' ) ) {
			return $this->error_response( 'File modifications are disabled' );
		}

		if ( ! isset( $args['plugin'] ) || empty( $args['plugin'] ) ) {
			return $this->error_response( 'Plugin identifier is required' );
		}

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

		$plugin = WordPress_Utils::find_plugin( $args['plugin'] );
		if ( ! $plugin ) {
			return $this->error_response( 'Plugin not found' );
		}

		$plugin_file = $plugin['file'];
		$plugin_name = $plugin['data']['Name'];

		// Deactivate first if active
		if ( is_plugin_active( $plugin_file ) ) {
			deactivate_plugins( $plugin_file );
		}

		// Delete the plugin
		$deleted = delete_plugins( array( $plugin_file ) );

		if ( is_wp_error( $deleted ) ) {
			return $this->error_response( 'Failed to uninstall plugin: ' . $deleted->get_error_message() );
		}

		return $this->success_response(
			'Plugin uninstalled successfully',
			array(
				'plugin_file' => $plugin_file,
				'name'        => $plugin_name,
			)
		);
	}
}
