<?php
/**
 * Get Embeddings Model Tool.
 *
 * Returns the currently configured embeddings model ID and provider information.
 *
 * @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;
}

/**
 * Get_Embeddings_Model_Tool class.
 */
class Get_Embeddings_Model_Tool extends Tool {

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

	/**
	 * Get the tool name.
	 *
	 * @return string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return 'Get Embeddings Model';
	}

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Get the currently configured embeddings model.';
	}

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

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

	// 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 ) {
		$llm_manager = LLM_Manager::get_instance();
		$model_id    = $llm_manager->get_embeddings_model_id();

		if ( empty( $model_id ) ) {
			return $this->success_response(
				'No embeddings model configured',
				array(
					'model_id'  => null,
					'provider'  => null,
					'dimension' => null,
				)
			);
		}

		$provider  = $llm_manager->get_provider_for_model( $model_id );
		$dimension = $llm_manager->get_cached_embeddings_model_dimension( $model_id );

		return $this->success_response(
			'Current embeddings model',
			array(
				'model_id'  => $model_id,
				'provider'  => $provider ? $provider->get_display_name() : null,
				'dimension' => $dimension ? $dimension : null,
			)
		);
	}
}
