<?php
/**
 * List Embeddings Models Tool.
 *
 * List all available embeddings models from enabled LLM providers including
 * model IDs and dimension 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://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;
}

/**
 * List_Embeddings_Models_Tool class.
 */
class List_Embeddings_Models_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'List all available embeddings models from enabled providers.';
	}

	/**
	 * 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(
				'limit'  => array(
					'type'        => 'integer',
					'description' => 'Maximum number of models to return. Default: 100.',
				),
				'offset' => array(
					'type'        => 'integer',
					'description' => 'Number of models to skip before returning results. Default: 0.',
				),
			),
			'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' );
	}

	/**
	 * 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 ) {
		$provider_manager = Provider_Manager::get_instance();
		$available_models = array();

		foreach ( $provider_manager->get_enabled_providers() as $provider ) {
			try {
				$models = $provider->get_available_models_cached();
				if ( ! empty( $models['embedding'] ) ) {
					foreach ( $models['embedding'] as $model ) {
						$available_models[] = array(
							'model_id' => $model['id'],
							'name'     => $model['name'] ?? $model['id'],
							'provider' => $provider->get_display_name(),
						);
					}
				}
			} catch ( \Throwable $e ) {
				continue;
			}
		}

		// Apply pagination
		$limit     = max( 1, $args['limit'] ?? 100 );
		$offset    = max( 0, $args['offset'] ?? 0 );
		$total     = count( $available_models );
		$paginated = array_slice( $available_models, $offset, $limit );
		$has_more  = $total > ( $offset + $limit );

		return $this->success_response(
			'Available embeddings models',
			array(
				'models'   => $paginated,
				'count'    => count( $paginated ),
				'total'    => $total,
				'limit'    => $limit,
				'offset'   => $offset,
				'has_more' => $has_more,
			)
		);
	}
}
