<?php
/**
 * Change Embeddings Model Tool.
 *
 * Switch the embeddings model used for semantic search. Triggers regeneration
 * of all embeddings when the model is changed.
 *
 * @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;
}

/**
 * Change_Embeddings_Model_Tool class.
 */
class Change_Embeddings_Model_Tool extends Tool {

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

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

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

	/**
	 * Get additional description instructions for the LLM.
	 *
	 * @return string Additional instructions appended to description.
	 * @since     1.0.0
	 */
	public function get_description_instructions() {
		return 'WARNING: Changing the model will trigger regeneration of all embeddings.';
	}

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

	/**
	 * 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(
				'model_id' => array(
					'type'        => 'string',
					'description' => 'The embeddings model ID to switch to',
				),
			),
			'required'   => array( 'model_id' ),
		);
	}

	/**
	 * Get the default enabled status for this tool.
	 *
	 * @return bool True if enabled by default, false otherwise.
	 * @since     1.0.0
	 */
	public function get_default_enabled_status() {
		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 current_user_can( 'manage_options' );
	}

	/**
	 * 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 ) {
		$llm_manager = LLM_Manager::get_instance();
		$model_id    = $args['model_id'];

		$provider = $llm_manager->get_provider_for_model( $model_id );
		if ( ! $provider ) {
			return $this->error_response( "Model not found or no configured provider supports it: {$model_id}" );
		}

		$old_model = $llm_manager->get_embeddings_model_id();
		$llm_manager->set_embeddings_model_id( $model_id );

		$embeddings_count = Embeddings_Manager::get_instance()->get_embeddings_count( 'content' );

		return $this->success_response(
			'Embeddings model changed. Embeddings will be regenerated automatically.',
			array(
				'old_model'                => $old_model,
				'new_model'                => $model_id,
				'provider'                 => $provider->get_display_name(),
				'embeddings_to_regenerate' => $embeddings_count,
			)
		);
	}
}
