<?php
/**
 * Import Tools Tool.
 *
 * Core tool that imports specified tools into the current session's context.
 * Imported tools have their definitions sent to the LLM for reference.
 *
 * @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;
}

/**
 * Import_Tools_Tool class.
 */
class Import_Tools_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Import tools into the current chat to add their definitions to context.';
	}

	/**
	 * 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(
				'tool_ids' => array(
					'type'        => 'array',
					'items'       => array( 'type' => 'string' ),
					'description' => 'Array of tool IDs to import into the current session',
				),
			),
			'required'   => array( 'tool_ids' ),
		);
	}

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

	/**
	 * 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 ) {
		$tool_ids     = $args['tool_ids'];
		$tool_manager = Tool_Manager::get_instance();

		$user_id = get_current_user_id();
		$thread  = Thread_Manager::get_instance()->get_or_create_thread( $user_id );

		$imported         = array();
		$already_imported = array();
		$not_found        = array();

		$current_imports = $thread->get_imported_tools();

		foreach ( $tool_ids as $tool_id ) {
			$tool = $tool_manager->get_tool( $tool_id );

			if ( ! $tool ) {
				$not_found[] = $tool_id;
				continue;
			}

			// Skip if already imported or always imported
			if ( $tool->is_always_imported() || in_array( $tool_id, $current_imports, true ) ) {
				$already_imported[] = $tool_id;
				continue;
			}

			$imported[] = $tool_id;
		}

		// Add newly imported tools to thread state
		if ( ! empty( $imported ) ) {
			$thread->add_imported_tools( $imported );
		}

		// Build message
		$parts = array();
		if ( ! empty( $imported ) ) {
			$parts[] = 'Imported: ' . count( $imported );
		}
		if ( ! empty( $already_imported ) ) {
			$parts[] = 'Already imported: ' . count( $already_imported );
		}
		if ( ! empty( $not_found ) ) {
			$parts[] = 'Not found: ' . count( $not_found );
		}

		return $this->success_response(
			implode( '. ', $parts ) . '.',
			array(
				'imported'         => $imported,
				'already_imported' => $already_imported,
				'not_found'        => $not_found,
			)
		);
	}
}
