<?php
/**
 * List Menus Tool.
 *
 * Allows the agent to retrieve a list of all navigation menus in WordPress
 * with item counts and location assignments.
 *
 * @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_Menus_Tool class.
 */
class List_Menus_Tool extends Tool {

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

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

	/**
	 * Get the description of the tool.
	 *
	 * @return     string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Lists all navigation menus in WordPress.';
	}

	/**
	 * Get LLM-specific instructions for this tool.
	 *
	 * @return     string Instructions for the LLM.
	 * @since     1.0.0
	 */
	public function get_instructions() {
		return 'TRUNCATION NOTE:
The `truncated_description` field is truncated for brevity. Use the `get_menu_details` tool with the menu ID to retrieve the full description and menu items.';
	}

	/**
	 * Get the parameters 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 menus to return',
					'default'     => 100,
				),
				'offset' => array(
					'type'        => 'integer',
					'description' => 'Number of menus to skip',
					'default'     => 0,
				),
			),
			'required'   => array(),
		);
	}

	/**
	 * Check if the current user has permission to execute this tool.
	 *
	 * @return     bool Whether the user has permission.
	 * @since     1.0.0
	 */
	public function current_user_can_execute() {
		return current_user_can( 'edit_theme_options' );
	}

	/**
	 * Check if this tool is read-only.
	 *
	 * @return     bool True - this tool only retrieves data.
	 * @since     1.0.0
	 */
	public function is_read_only() {
		return true;
	}

	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param      array $args The arguments for the tool.
	 * @return     array The result of executing the tool.
	 * @since     1.0.0
	 */
	protected function execute( $args ) {
		$menus = wp_get_nav_menus();

		$limit  = isset( $args['limit'] ) ? intval( $args['limit'] ) : 100;
		$offset = isset( $args['offset'] ) ? intval( $args['offset'] ) : 0;

		if ( empty( $menus ) ) {
			return $this->success_response(
				'No menus found',
				array(
					'menus'    => array(),
					'count'    => 0,
					'total'    => 0,
					'limit'    => $limit,
					'offset'   => $offset,
					'has_more' => false,
				)
			);
		}

		// Apply pagination
		$total_menus = count( $menus );
		$menus       = array_slice( $menus, $offset, $limit );

		$menu_list = array();
		foreach ( $menus as $menu ) {
			$menu_list[] = array(
				'id'                    => $menu->term_id,
				'name'                  => $menu->name,
				'slug'                  => $menu->slug,
				'truncated_description' => String_Utils::truncate_to_token_boundary_with_ellipsis( $menu->description, 200 ),
				'count'                 => $menu->count,
			);
		}

		$has_more = ( $offset + count( $menus ) ) < $total_menus;

		return $this->success_response(
			'Menus retrieved successfully',
			array(
				'menus'    => $menu_list,
				'count'    => count( $menu_list ),
				'total'    => $total_menus,
				'limit'    => $limit,
				'offset'   => $offset,
				'has_more' => $has_more,
			)
		);
	}
}
