<?php
/**
 * Delete Comment Tool.
 *
 * Delete comments by ID using wp_delete_comment() or wp_trash_comment()
 * with support for trash or permanent deletion modes.
 *
 * @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;
}

/**
 * Delete_Comment_Tool class.
 */
class Delete_Comment_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Delete a comment (move to trash or permanently delete).';
	}

	/**
	 * Get usage instructions for the LLM.
	 *
	 * @return string Instructions for using this tool.
	 * @since     1.0.0
	 */
	public function get_instructions() {
		return 'DELETION MODES:
- force_delete=false (default): Moves comment to trash. Can be restored later.
- force_delete=true: Permanently deletes comment. Cannot be undone.

BEHAVIOR:
- Trashed comments can be restored via the `update_comment` tool with status="approve" or status="hold".
- Permanently deleted comments are removed from the database entirely.
- Consider using the `update_comment` tool with status="trash" if you might need to restore later.

COMMON PATTERNS:
- Trash a comment: { id: 123 }
- Permanently delete: { id: 123, force_delete: true }';
	}

	/**
	 * 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(
				'id'           => array(
					'type'        => 'integer',
					'description' => 'The ID of the comment to delete.',
				),
				'force_delete' => array(
					'type'        => 'boolean',
					'description' => 'Permanently delete instead of moving to trash.',
					'default'     => false,
				),
			),
			'required'   => array( 'id' ),
		);
	}

	/**
	 * 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( 'moderate_comments' );
	}

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

	/**
	 * 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 ) {
		if ( ! isset( $args['id'] ) || empty( $args['id'] ) ) {
			return $this->error_response( 'Comment ID is required.' );
		}

		$comment_id = intval( $args['id'] );
		$comment    = get_comment( $comment_id );

		if ( ! $comment ) {
			return $this->error_response(
				'Comment not found.',
				array( 'hint' => 'Verify the comment ID exists using the `list_comments` tool.' )
			);
		}

		$force_delete = isset( $args['force_delete'] ) && true === $args['force_delete'];
		$post_id      = intval( $comment->comment_post_ID );
		$author       = $comment->comment_author;

		$deleted = wp_delete_comment( $comment_id, $force_delete );

		if ( ! $deleted ) {
			return $this->error_response( 'Failed to delete comment.' );
		}

		$message = $force_delete ? 'Comment permanently deleted.' : 'Comment moved to trash.';

		return $this->success_response(
			$message,
			array(
				'comment_id'    => $comment_id,
				'post_id'       => $post_id,
				'author'        => $author,
				'force_deleted' => $force_delete,
			)
		);
	}
}
