<?php
/**
 * Delete Custom Post Tool.
 *
 * Allows the agent to delete posts of any post type. Includes validation,
 * permission checks, and trash/permanent deletion options.
 *
 * @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;
}

/**
 * Delete_Custom_Post_Tool class.
 */
class Delete_Custom_Post_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Delete a post of any post type.';
	}

	/**
	 * Get additional description instructions for the LLM.
	 *
	 * @return string Additional instructions appended to description.
	 * @since     1.0.0
	 */
	public function get_description_instructions() {
		return 'Moves to trash by default, or permanently deletes if `force_delete` is 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 post to delete. Required.',
				),
				'force_delete' => array(
					'type'        => 'boolean',
					'description' => 'Whether to permanently delete the post 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( 'delete_posts' );
	}

	/**
	 * 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( 'Post ID is required' );
		}

		$post_id = intval( $args['id'] );
		$post    = get_post( $post_id );

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

		$post_type     = $post->post_type;
		$post_type_obj = get_post_type_object( $post_type );

		if ( ! $post_type_obj ) {
			return $this->error_response( 'Invalid post type' );
		}

		if ( ! current_user_can( 'delete_post', $post_id ) ) {
			return $this->error_response( 'You do not have permission to delete this post' );
		}

		$force_delete = isset( $args['force_delete'] ) && true === $args['force_delete'];

		// Store post info for response before deletion
		$post_title = $post->post_title;

		$deleted = wp_delete_post( $post_id, $force_delete );

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

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

		return $this->success_response(
			$message,
			array(
				'post_id'         => $post_id,
				'post_type'       => $post_type,
				'post_type_label' => $post_type_obj->labels->singular_name,
				'post_title'      => $post_title,
				'force_deleted'   => $force_delete,
			)
		);
	}
}
