<?php
/**
 * List Menu Locations Tool.
 *
 * Allows the agent to list all registered theme menu locations and their
 * current menu 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_Menu_Locations_Tool class.
 */
class List_Menu_Locations_Tool extends Tool {

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

	/**
	 * Get the tool name.
	 *
	 * @return     string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return __( 'List Menu Locations', '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 registered menu locations and their assigned menus.';
	}

	/**
	 * 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(),
			'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.
	 */
	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- required by abstract Tool::execute()
	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param array $args The tool arguments.
	 *
	 * @return array The tool execution result.
	 * @since     1.0.0
	 */
	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Required by abstract method signature.
	protected function execute( $args ) {
		$registered_locations = get_registered_nav_menus();

		// Check if theme supports classic menus (block themes typically don't register menu locations)
		if ( empty( $registered_locations ) ) {
			$is_block_theme = function_exists( 'wp_is_block_theme' ) && wp_is_block_theme();
			if ( $is_block_theme ) {
				return $this->error_response(
					\__( 'This theme is a block theme and does not use classic menu locations. Block themes use the Navigation block instead. Use the list_menus tool to see available menus that can be used with Navigation blocks.', 'agenistrator' )
				);
			}
			return $this->success_response(
				\__( 'No menu locations are registered by the current theme.', 'agenistrator' ),
				array( 'locations' => array() )
			);
		}

		$assigned_locations = get_nav_menu_locations();

		$locations = array();
		foreach ( $registered_locations as $location => $description ) {
			$menu_id   = $assigned_locations[ $location ] ?? 0;
			$menu_name = '';

			if ( $menu_id ) {
				$menu = wp_get_nav_menu_object( $menu_id );
				if ( $menu ) {
					$menu_name = $menu->name;
				}
			}

			$locations[] = array(
				'location'    => $location,
				'description' => $description,
				'menu_id'     => $menu_id,
				'menu_name'   => $menu_name,
				'has_menu'    => $menu_id > 0,
			);
		}

		return $this->success_response( 'Menu locations retrieved successfully', array( 'locations' => $locations ) );
	}
}
