<?php
/**
 * Get Menu Details Tool.
 *
 * Allows the agent to get details of a navigation menu including all its items,
 * hierarchy, and assigned locations.
 *
 * @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;
}

/**
 * Get_Menu_Details_Tool class.
 */
class Get_Menu_Details_Tool extends Tool {

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

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

	/**
	 * Get the description of the tool.
	 *
	 * @return     string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Gets details of a navigation menu including all its 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(
				'menu_id' => array(
					'type'        => 'integer',
					'description' => 'The ID of the menu to get details for',
				),
			),
			'required'   => array( 'menu_id' ),
		);
	}

	/**
	 * 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 ) {
		$menu_id = intval( $args['menu_id'] );

		$menu = wp_get_nav_menu_object( $menu_id );
		if ( ! $menu ) {
			return $this->error_response( "Menu with ID {$menu_id} not found." );
		}

		$menu_items = wp_get_nav_menu_items( $menu_id );
		$items      = array();

		if ( ! empty( $menu_items ) ) {
			foreach ( $menu_items as $item ) {
				$items[] = array(
					'id'          => $item->ID,
					'title'       => $item->title,
					'url'         => $item->url,
					'type'        => $item->type,
					'object'      => $item->object,
					'object_id'   => intval( $item->object_id ),
					'parent_id'   => intval( $item->menu_item_parent ),
					'menu_order'  => $item->menu_order,
					'target'      => $item->target,
					'classes'     => $item->classes,
					'description' => $item->description,
					'attr_title'  => $item->attr_title,
					'xfn'         => $item->xfn,
				);
			}
		}

		// Get assigned locations.
		$locations          = get_nav_menu_locations();
		$assigned_locations = array();
		foreach ( $locations as $location => $assigned_menu_id ) {
			if ( $assigned_menu_id === $menu_id ) {
				$assigned_locations[] = $location;
			}
		}

		return $this->success_response(
			'Menu details retrieved successfully',
			array(
				'menu'               => array(
					'id'          => $menu->term_id,
					'name'        => $menu->name,
					'slug'        => $menu->slug,
					'description' => $menu->description,
					'count'       => $menu->count,
				),
				'items'              => $items,
				'assigned_locations' => $assigned_locations,
			)
		);
	}
}
