<?php
/**
 * Get Comment Details Tool.
 *
 * Retrieve detailed information about a specific comment including author,
 * content, status, parent, and associated post metadata.
 *
 * @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;
}

/**
 * Get_Comment_Details_Tool class.
 */
class Get_Comment_Details_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Get detailed information about a specific comment by ID.';
	}

	/**
	 * 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 retrieve',
				),
				'content_offset'   => array(
					'type'        => 'integer',
					'description' => 'Byte offset for content chunking (use 0 for first chunk, then use content_offset + content_size from response for next chunk)',
					'default'     => 0,
				),
				'content_max_size' => array(
					'type'        => 'integer',
					'description' => 'Maximum size of content chunk in bytes',
					'default'     => 8192,
				),
			),
			'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 true;
	}

	/**
	 * 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 = get_comment( intval( $args['id'] ) );

		if ( ! $comment ) {
			return $this->error_response( 'Comment not found' );
		}

		$post = get_post( $comment->comment_post_ID );

		// Get chunking parameters
		$content_offset   = isset( $args['content_offset'] ) ? intval( $args['content_offset'] ) : 0;
		$content_max_size = isset( $args['content_max_size'] ) ? intval( $args['content_max_size'] ) : 8192;

		// Chunk the content using token-boundary splitting
		$content_chunk = String_Utils::split_on_token_boundary(
			$comment->comment_content,
			$content_offset,
			$content_max_size
		);

		$comment_data = array(
			'id'                 => $comment->comment_ID,
			'post_id'            => $comment->comment_post_ID,
			'post_title'         => $post ? $post->post_title : '',
			'post_url'           => $post ? get_permalink( $post ) : '',
			'author'             => $comment->comment_author,
			'author_email'       => $comment->comment_author_email,
			'author_url'         => $comment->comment_author_url,
			'author_ip'          => $comment->comment_author_IP,
			'date'               => $comment->comment_date,
			'date_gmt'           => $comment->comment_date_gmt,
			'content'            => $content_chunk['content'],
			'content_total_size' => $content_chunk['total_size'],
			'content_offset'     => $content_chunk['offset'],
			'content_size'       => $content_chunk['size'],
			'content_has_more'   => $content_chunk['has_more'],
			'karma'              => $comment->comment_karma,
			'status'             => wp_get_comment_status( $comment ),
			'agent'              => $comment->comment_agent,
			'type'               => $comment->comment_type,
			'parent'             => $comment->comment_parent,
			'user_id'            => $comment->user_id,
		);

		// Get parent comment info if exists
		if ( $comment->comment_parent ) {
			$parent = get_comment( $comment->comment_parent );
			if ( $parent ) {
				$comment_data['parent_info'] = array(
					'id'      => $parent->comment_ID,
					'author'  => $parent->comment_author,
					'excerpt' => wp_trim_words( $parent->comment_content, 20 ),
				);
			}
		}

		// Get reply count
		$replies                     = get_comments(
			array(
				'parent' => $comment->comment_ID,
				'count'  => true,
			)
		);
		$comment_data['reply_count'] = $replies;

		return $this->success_response(
			'Comment retrieved successfully',
			array(
				'comment' => $comment_data,
			)
		);
	}
}
