<?php
/**
 * Get Post Meta Tool.
 *
 * Retrieve the value of a specific meta key from a post, page, or custom post
 * type. Returns single or multiple values based on the key structure.
 *
 * @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;
}

/**
 * Get_Post_Meta_Tool class.
 */
class Get_Post_Meta_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Get the value of a specific meta key from a post, page, or custom post type. Works with any content type that uses post meta.';
	}

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

	/**
	 * 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 get 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 retrieve.',
				),
				'single'   => array(
					'type'        => 'boolean',
					'description' => 'If true, returns a single value. If false, returns an array of all values for this key. Default: true.',
					'default'     => true,
				),
			),
			'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( 'read' );
	}

	/**
	 * 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'] );
		$single   = isset( $args['single'] ) ? (bool) $args['single'] : true;

		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( 'read_post', $post_id ) ) {
			return $this->error_response( 'You do not have permission to read this post' );
		}

		$meta_value = get_post_meta( $post_id, $meta_key, $single );

		// Check if meta key exists
		if ( $single && '' === $meta_value ) {
			// For single values, empty string could mean no value or actual empty string
			// Check if key exists at all
			$all_values = get_post_meta( $post_id, $meta_key, false );
			if ( empty( $all_values ) ) {
				return $this->success_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,
						'exists'   => false,
						'value'    => null,
					)
				);
			}
		}

		if ( ! $single && empty( $meta_value ) ) {
			return $this->success_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,
					'exists'   => false,
					'value'    => array(),
				)
			);
		}

		return $this->success_response(
			'Post meta retrieved successfully',
			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,
				'exists'    => true,
				'value'     => $meta_value,
				'single'    => $single,
			)
		);
	}
}
