<?php
/**
 * Add Menu Item Tool.
 *
 * Allows the agent to add an item to a navigation menu. Supports custom links,
 * posts, pages, taxonomy terms, and parent-child relationships.
 *
 * @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://maximum.software/
 *
 * 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' ) ) {
	return;
}

/**
 * Add_Menu_Item_Tool class.
 */
class Add_Menu_Item_Tool extends Tool {

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

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

	/**
	 * Get the description of the tool.
	 *
	 * @return     string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Adds an item to a navigation menu.';
	}

	/**
	 * Get additional description instructions for the LLM.
	 *
	 * @return string Additional instructions appended to description.
	 * @since     1.0.0
	 */
	public function get_description_instructions() {
		return 'Supports custom links, pages, posts, categories, and other content types.';
	}

	/**
	 * 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 add the item to',
				),
				'title'     => array(
					'type'        => 'string',
					'description' => 'The title/label of the menu item',
				),
				'type'      => array(
					'type'        => 'string',
					'description' => 'The type of menu item: "custom", "post_type", or "taxonomy"',
					'enum'        => array( 'custom', 'post_type', 'taxonomy' ),
				),
				'url'       => array(
					'type'        => 'string',
					'description' => 'The URL for custom link items in valid URL format. Required when type is "custom"',
				),
				'object'    => array(
					'type'        => 'string',
					'description' => 'The object type (e.g., "page", "post", "category"). Required for post_type and taxonomy types',
				),
				'object_id' => array(
					'type'        => 'integer',
					'description' => 'The ID of the object (post, page, category, etc.). Required for post_type and taxonomy types',
				),
				'parent_id' => array(
					'type'        => 'integer',
					'description' => 'The ID of the parent menu item for creating submenus',
				),
				'position'  => array(
					'type'        => 'integer',
					'description' => 'The position/order of the menu item',
				),
				'target'    => array(
					'type'        => 'string',
					'description' => 'Link target (e.g., "_blank" for new window)',
				),
				'classes'   => array(
					'type'        => 'array',
					'items'       => array( 'type' => 'string' ),
					'description' => 'CSS classes for the menu item',
				),
			),
			'required'   => array( 'menu_id', 'title', 'type' ),
		);
	}

	/**
	 * 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 False - this tool modifies data.
	 * @since     1.0.0
	 */
	public function is_read_only() {
		return false;
	}

	/**
	 * 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." );
		}

		$type = sanitize_text_field( $args['type'] );

		// Validate required fields based on type.
		if ( 'custom' === $type && empty( $args['url'] ) ) {
			return $this->error_response( 'URL is required for custom link items.' );
		}

		if ( in_array( $type, array( 'post_type', 'taxonomy' ), true ) ) {
			if ( empty( $args['object'] ) || empty( $args['object_id'] ) ) {
				return $this->error_response( 'Object and object_id are required for post_type and taxonomy items.' );
			}
		}

		$menu_item_data = array(
			'menu-item-title'     => sanitize_text_field( $args['title'] ),
			'menu-item-status'    => 'publish',
			'menu-item-type'      => $type,
			'menu-item-url'       => isset( $args['url'] ) ? esc_url_raw( $args['url'] ) : '',
			'menu-item-object'    => isset( $args['object'] ) ? sanitize_text_field( $args['object'] ) : '',
			'menu-item-object-id' => isset( $args['object_id'] ) ? intval( $args['object_id'] ) : 0,
			'menu-item-parent-id' => isset( $args['parent_id'] ) ? intval( $args['parent_id'] ) : 0,
			'menu-item-position'  => isset( $args['position'] ) ? intval( $args['position'] ) : 0,
			'menu-item-target'    => isset( $args['target'] ) ? sanitize_text_field( $args['target'] ) : '',
			'menu-item-classes'   => isset( $args['classes'] ) ? implode( ' ', array_map( 'sanitize_html_class', $args['classes'] ) ) : '',
		);

		$item_id = wp_update_nav_menu_item( $menu_id, 0, $menu_item_data );

		if ( is_wp_error( $item_id ) ) {
			return $this->error_response( $item_id->get_error_message() );
		}

		return $this->success_response(
			'Menu item added successfully',
			array(
				'item_id' => $item_id,
				'menu_id' => $menu_id,
				'title'   => $args['title'],
			)
		);
	}
}
