<?php
/**
 * Unimport Tools Tool.
 *
 * Core tool that removes specified tools from the current session's context.
 * Unimported tools will no longer have their definitions sent to the LLM.
 *
 * @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;
}

/**
 * Unimport_Tools_Tool class.
 */
class Unimport_Tools_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Remove tools from the current chat to reduce context size.';
	}

	/**
	 * 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 remove from 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 );

		$unimported      = array();
		$not_imported    = array();
		$always_imported = array();

		$current_imports = $thread->get_imported_tools();

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

			// Check if tool is always imported (core tools)
			if ( $tool && $tool->is_always_imported() ) {
				$always_imported[] = $tool_id;
				continue;
			}

			// Check if tool is currently imported
			if ( ! in_array( $tool_id, $current_imports, true ) ) {
				$not_imported[] = $tool_id;
				continue;
			}

			$unimported[] = $tool_id;
		}

		// Remove unimported tools from thread state
		if ( ! empty( $unimported ) ) {
			$thread->remove_imported_tools( $unimported );
		}

		// Build message
		$parts = array();
		if ( ! empty( $unimported ) ) {
			$parts[] = 'Unimported: ' . count( $unimported );
		}
		if ( ! empty( $not_imported ) ) {
			$parts[] = 'Not imported: ' . count( $not_imported );
		}
		if ( ! empty( $always_imported ) ) {
			$parts[] = 'Cannot unimport: ' . count( $always_imported );
		}

		return $this->success_response(
			implode( '. ', $parts ) . '.',
			array(
				'unimported'      => $unimported,
				'not_imported'    => $not_imported,
				'always_imported' => $always_imported,
			)
		);
	}
}
