<?php
/**
 * Change Chat Position Tool.
 *
 * Change the position of the chat widget in the WordPress admin.
 * Supports sidebar or bottom-right floating positions.
 *
 * @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;
}

/**
 * Change_Chat_Position_Tool class.
 */
class Change_Chat_Position_Tool extends Tool {

	private static $valid_positions = array( 'bottom-right', 'bottom-left', 'top-right', 'top-left' );

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Change the position of the chat widget in the WordPress admin.';
	}

	/**
	 * Get additional description instructions for the LLM.
	 *
	 * @return string Additional instructions appended to description.
	 * @since     1.0.0
	 */
	public function get_description_instructions() {
		return 'Valid positions: bottom-right, bottom-left, top-right, top-left.';
	}

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

	/**
	 * Get the parameters schema 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(
				'position' => array(
					'type'        => 'string',
					'enum'        => self::$valid_positions,
					'description' => 'The new position for the chat widget.',
				),
			),
			'required'   => array( 'position' ),
		);
	}

	/**
	 * Get the default enabled status for this tool.
	 *
	 * @return bool True if enabled by default, false otherwise.
	 * @since     1.0.0
	 */
	public function get_default_enabled_status() {
		return true;
	}

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

	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param array $args The tool arguments.
	 *
	 * @return array The tool execution result.
	 * @since     1.0.0
	 */
	protected function execute( $args ) {
		$position = $args['position'];

		if ( ! in_array( $position, self::$valid_positions, true ) ) {
			return $this->error_response( 'Invalid position. Must be one of: ' . implode( ', ', self::$valid_positions ) );
		}

		$settings     = Settings::get_instance();
		$old_position = $settings->get_config( 'chat_position', 'bottom-right' );

		$settings->set_config( 'chat_position', $position );
		$settings->save_config();

		return $this->success_response(
			'Chat position changed',
			array(
				'old_position' => $old_position,
				'new_position' => $position,
			)
		);
	}
}
