<?php
/**
 * Create Menu Tool.
 *
 * Allows the agent to create a new navigation menu and optionally assign it
 * to a theme location.
 *
 * @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;
}

/**
 * Create_Menu_Tool class.
 */
class Create_Menu_Tool extends Tool {

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

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

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

	/**
	 * 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(
				'name'        => array(
					'type'        => 'string',
					'description' => 'The name of the menu to create',
				),
				'description' => array(
					'type'        => 'string',
					'description' => 'Optional description for the menu in plain text format',
				),
			),
			'required'   => array( 'name' ),
		);
	}

	/**
	 * 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 ) {
		$name        = sanitize_text_field( $args['name'] );
		$description = isset( $args['description'] ) ? sanitize_text_field( $args['description'] ) : '';

		// Check if menu already exists.
		$existing = wp_get_nav_menu_object( $name );
		if ( $existing ) {
			return $this->error_response( "A menu with the name '{$name}' already exists." );
		}

		$menu_id = wp_create_nav_menu( $name );

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

		// Update description if provided.
		if ( ! empty( $description ) ) {
			wp_update_term( $menu_id, 'nav_menu', array( 'description' => $description ) );
		}

		$menu = wp_get_nav_menu_object( $menu_id );

		return $this->success_response(
			'Menu created successfully',
			array(
				'menu' => array(
					'id'          => $menu->term_id,
					'name'        => $menu->name,
					'slug'        => $menu->slug,
					'description' => $menu->description,
				),
			)
		);
	}
}
