<?php
/** Read ability for scalar values on normal Pods post types and taxonomies. */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function wpagent_ability_get_pods_field_values(): array {
	return array(
		'label' => __( 'Pods-Feldwerte lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest die vollständigen Werte freigegebener, nicht wiederholbarer Pods-Metafelder eines Beitrags oder Begriffs über Pods raw(). Beziehungen, Medien und andere Feldtypen bleiben ausgeschlossen.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object',
			'properties' => array(
				'model_name' => array( 'type' => 'string', 'pattern' => '^[a-z][a-z0-9_]{0,63}$' ),
				'object_type' => array( 'type' => 'string', 'enum' => array( 'post', 'term' ) ),
				'object_id' => array( 'type' => 'integer', 'minimum' => 1 ),
			),
			'required' => array( 'model_name', 'object_type', 'object_id' ),
			'additionalProperties' => false,
		),
		'output_schema' => wpagent_pods_meta_values_state_schema(),
		'execute_callback' => 'wpagent_exec_get_pods_field_values',
		'permission_callback' => 'wpagent_perm_pods_meta_values',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_pods_field_values( $input ) {
	$input = is_array( $input ) ? $input : array();
	$context = wpagent_pods_meta_values_context( $input['model_name'] ?? null, $input['object_type'] ?? null, $input['object_id'] ?? null );
	if ( is_wp_error( $context ) ) {
		return $context;
	}
	if ( ! wpagent_pods_meta_values_can_edit( $context ) ) {
		return new WP_Error( 'wpagent_pods_meta_values_forbidden', __( 'Du darfst die Pods-Werte dieses Objekts nicht lesen.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	return wpagent_pods_meta_values_state( $context );
}
