<?php
/**
 * List All Tools Tool.
 *
 * Core tool that returns a list of all available tools with their ID, name,
 * and description. Used by the LLM to discover what tools are available before
 * importing them.
 *
 * @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;
}

/**
 * List_All_Tools_Tool class.
 */
class List_All_Tools_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'List all available tools with their ID, name, and description.';
	}

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

	/**
	 * Check if this tool is always enabled.
	 *
	 * @return bool True if always enabled.
	 * @since     1.0.0
	 */
	public function is_always_enabled() {
		return true;
	}

	/**
	 * Check if this tool is always imported into context.
	 *
	 * @return bool True if always imported.
	 * @since     1.0.0
	 */
	public function is_always_imported() {
		return true;
	}

	/**
	 * 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 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 ) {
		$tools     = Tool_Manager::get_instance()->get_tools();
		$tool_list = array();

		foreach ( $tools as $tool ) {
			$description              = $tool->get_description();
			$description_instructions = $tool->get_description_instructions();
			if ( '' !== $description_instructions ) {
				$description .= ' ' . $description_instructions;
			}

			$tool_list[] = array(
				'id'          => $tool->get_id(),
				'description' => $description,
			);
		}

		return $this->success_response(
			'Tools retrieved successfully',
			array(
				'tools'       => $tool_list,
				'total_count' => count( $tool_list ),
			)
		);
	}
}
