<?php
/**
 * Delete Post Meta Tool.
 *
 * Delete a specific meta key from a post, page, or custom post type. Supports
 * deleting specific values or all values for a key.
 *
 * @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_Meta_Tool class.
 */
class Delete_Post_Meta_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Delete a meta key from a post, page, or custom post type. Can optionally delete only entries with a specific value.';
	}

	/**
	 * 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(
				'post_id'    => array(
					'type'        => 'integer',
					'description' => 'The ID of the post to delete meta from.',
				),
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Array key for tool parameter, not a database query.
				'meta_key'   => array(
					'type'        => 'string',
					'description' => 'The meta key to delete.',
				),
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Array key for tool parameter, not a database query.
				'meta_value' => array(
					'type'        => 'string',
					'description' => 'Optional. If specified, only delete entries with this exact value. Useful when multiple entries exist for the same key.',
				),
			),
			'required'   => array( 'post_id', 'meta_key' ),
		);
	}

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

	/**
	 * 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 ) {
		$post_id  = (int) $args['post_id'];
		$meta_key = trim( $args['meta_key'] );

		if ( '' === $meta_key ) {
			return $this->error_response( 'Meta key cannot be empty' );
		}

		$post = get_post( $post_id );

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

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

		$old_value = get_post_meta( $post_id, $meta_key, false );

		if ( false === $old_value || array() === $old_value ) {
			return $this->error_response(
				'Meta key not found on this post',
				array(
					'post_id'  => $post_id,
					// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Array key for response data, not a database query.
					'meta_key' => $meta_key,
				)
			);
		}

		$meta_value = isset( $args['meta_value'] ) ? $args['meta_value'] : '';

		if ( '' !== $meta_value ) {
			$result = delete_post_meta( $post_id, $meta_key, $meta_value );
		} else {
			$result = delete_post_meta( $post_id, $meta_key );
		}

		if ( ! $result ) {
			return $this->error_response(
				'Failed to delete post meta',
				array(
					'post_id'  => $post_id,
					// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Array key for response data, not a database query.
					'meta_key' => $meta_key,
				)
			);
		}

		$response_data = array(
			'post_id'   => $post_id,
			'post_type' => $post->post_type,
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Array key for response data, not a database query.
			'meta_key'  => $meta_key,
			'old_value' => $old_value,
		);

		if ( '' !== $meta_value ) {
			$response_data['deleted_value'] = $meta_value;
		}

		return $this->success_response( 'Post meta deleted successfully', $response_data );
	}
}
