<?php
/**
 * Delete Post Tool.
 *
 * Allows the agent to delete WordPress posts by ID or slug. 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_Post_Tool class.
 */
class Delete_Post_Tool extends Tool {

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

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

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

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

	/**
	 * Check if this tool is read-only.
	 *
	 * @return     bool False - this tool deletes content.
	 * @since     1.0.0
	 */
	public function is_read_only() {
		return false;
	}

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

	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param      array $args The arguments for the tool.
	 * @return     array The result of executing the tool.
	 * @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' );
		}

		// Check if post type is 'post'
		if ( 'post' !== $post->post_type ) {
			return $this->error_response( 'The specified ID is not a post' );
		}

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

		// Determine if we should force delete or move to trash
		$force_delete = isset( $args['force_delete'] ) && true === $args['force_delete'];

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

		// Delete the post
		$deleted = wp_delete_post( $post_id, $force_delete );

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

		if ( $force_delete ) {
			$message = 'Post permanently deleted';
		} else {
			$message = 'Post moved to trash';
		}

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