<?php
/**
 * List Post Meta Tool.
 *
 * List all meta keys and their values for a post, page, or custom post type.
 * Can optionally filter internal/protected meta keys.
 *
 * @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;
}

/**
 * List_Post_Meta_Tool class.
 */
class List_Post_Meta_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'List all meta keys and their values for 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 list meta from.',
				),
			),
			'required'   => array( 'post_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( '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'];

		$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' );
		}

		// Get all post meta - passing empty string returns all meta
		$all_meta = get_post_meta( $post_id, '', false );

		if ( false === $all_meta ) {
			return $this->error_response( 'Failed to retrieve post meta' );
		}

		$meta_data = array();

		foreach ( $all_meta as $meta_key => $values ) {
			// Unserialize values - get_post_meta with empty key returns raw serialized data
			$values = array_map( 'maybe_unserialize', $values );

			// Flatten single-value arrays for readability
			$meta_data[ $meta_key ] = ( 1 === count( $values ) ) ? $values[0] : $values;
		}

		return $this->success_response(
			'Post meta retrieved successfully',
			array(
				'post_id'   => $post_id,
				'post_type' => $post->post_type,
				'meta'      => $meta_data,
				'count'     => count( $meta_data ),
			)
		);
	}
}
