<?php
/**
 * Search Plugins Tool.
 *
 * Allows the agent to search the WordPress.org plugin directory using the
 * plugins_api with search term, author, or tag filters.
 *
 * @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;
}

/**
 * Search_Plugins_Tool class.
 */
class Search_Plugins_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Search the WordPress.org plugin directory.';
	}

	/**
	 * 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(
				'search'   => array(
					'type'        => 'string',
					'description' => 'Search term',
				),
				'per_page' => array(
					'type'        => 'integer',
					'description' => 'Number of results per page',
					'default'     => 10,
				),
			),
			'required'   => array( 'search' ),
		);
	}

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

		if ( ! function_exists( 'plugins_api' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
		}

		$per_page = isset( $args['per_page'] ) ? intval( $args['per_page'] ) : 10;

		$api_result = plugins_api(
			'query_plugins',
			array(
				'search'   => sanitize_text_field( $args['search'] ),
				'per_page' => $per_page,
				'fields'   => array(
					'short_description' => true,
					'icons'             => false,
					'banners'           => false,
					'compatibility'     => false,
				),
			)
		);

		if ( is_wp_error( $api_result ) ) {
			return $this->error_response( 'Failed to search plugins: ' . $api_result->get_error_message() );
		}

		$plugins = array();
		foreach ( $api_result->plugins as $p ) {
			$plugins[] = array(
				'slug'              => $p['slug'],
				'name'              => $p['name'],
				'version'           => $p['version'],
				'author'            => wp_strip_all_tags( $p['author'] ),
				'rating'            => $p['rating'],
				'active_installs'   => $p['active_installs'],
				'short_description' => $p['short_description'],
			);
		}

		return $this->success_response(
			'Plugin search completed',
			array(
				'plugins' => $plugins,
				'total'   => $api_result->info['results'],
			)
		);
	}
}
