<?php
/**
 * Sync Embeddings Tool.
 *
 * Trigger embedding synchronization for site content. Supports regenerating
 * all embeddings or syncing only modified content.
 *
 * @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;
}

/**
 * Sync_Embeddings_Tool class.
 */
class Sync_Embeddings_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Trigger embedding sync for site content. Can regenerate all embeddings or only sync modified content.';
	}

	/**
	 * 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(
				'regenerate_all' => array(
					'type'        => 'boolean',
					'description' => 'If true, regenerate all embeddings. If false (default), only sync modified content.',
				),
			),
			'required'   => array(),
		);
	}

	/**
	 * 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 ) {
		$regenerate_all = $args['regenerate_all'] ?? false;

		if ( ! LLM_Manager::get_instance()->is_embeddings_model_configured() ) {
			return $this->error_response( 'No embeddings model is configured. Configure an embedding model first.' );
		}

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

		// Manual sync: walk all posts (since_last_sync=false) and always run cleanup
		$embeddings_manager->schedule_sync_content_embeddings( $regenerate_all, true, false );

		$action = $regenerate_all ? 'Regeneration of all embeddings' : 'Sync of modified embeddings';

		return $this->success_response(
			"{$action} has been scheduled",
			array(
				'regenerate_all'           => $regenerate_all,
				'current_embeddings_count' => $current_count,
				'embeddings_model'         => LLM_Manager::get_instance()->get_embeddings_model_id(),
			)
		);
	}
}
