<?php
/** Read-only native record abilities for bounded Pods Advanced Content Types. */

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

/** @return array<string,mixed> */
function wpagent_pods_act_field_output_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'name' => array( 'type' => 'string' ), 'type' => array( 'type' => 'string', 'enum' => array( 'text' ) ),
			'required' => array( 'type' => 'boolean' ), 'max_length' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => WPAGENT_PODS_ACT_MAX_TEXT ),
		),
		'required' => array( 'name', 'type', 'required', 'max_length' ), 'additionalProperties' => false,
	);
}

/** @return array<string,mixed> */
function wpagent_pods_act_record_output_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'id' => array( 'type' => 'integer', 'minimum' => 1 ),
			'values' => array( 'type' => 'object', 'maxProperties' => WPAGENT_PODS_ACT_MAX_FIELDS, 'additionalProperties' => array( 'type' => 'string' ) ),
			'hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
		'required' => array( 'id', 'values', 'hash' ), 'additionalProperties' => false,
	);
}

function wpagent_ability_list_pods_act_records(): array {
	return array(
		'label'       => __( 'Pods Advanced-Content-Type-Datensätze auflisten', 'wp-agent-companion' ),
		'description' => __( 'Listet Datensätze eines bestehenden, standardmäßigen tabellenbasierten Pods Advanced Content Type über die native Pods-API. Nur nicht wiederholbare Textfelder sind eingeschlossen.', '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}$' ),
				'page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => WPAGENT_PODS_ACT_MAX_PAGE, 'default' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => WPAGENT_PODS_ACT_MAX_PER_PAGE, 'default' => 20 ),
			),
			'required' => array( 'model_name' ), 'additionalProperties' => false,
		),
		'output_schema' => array(
			'type' => 'object', 'properties' => array(
				'model_name' => array( 'type' => 'string' ), 'blog_id' => array( 'type' => 'integer', 'minimum' => 1 ),
				'schema_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
				'fields' => array( 'type' => 'array', 'maxItems' => WPAGENT_PODS_ACT_MAX_FIELDS, 'items' => wpagent_pods_act_field_output_schema() ),
				'records' => array( 'type' => 'array', 'maxItems' => WPAGENT_PODS_ACT_MAX_PER_PAGE, 'items' => wpagent_pods_act_record_output_schema() ),
				'total' => array( 'type' => 'integer', 'minimum' => 0 ), 'page' => array( 'type' => 'integer', 'minimum' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1 ), 'total_pages' => array( 'type' => 'integer', 'minimum' => 0 ),
			),
			'required' => array( 'model_name', 'blog_id', 'schema_hash', 'fields', 'records', 'total', 'page', 'per_page', 'total_pages' ), 'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_list_pods_act_records',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

function wpagent_ability_get_pods_act_record(): array {
	return array(
		'label'       => __( 'Pods Advanced-Content-Type-Datensatz lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest einen tabellenbasierten Pods Advanced-Content-Type-Datensatz einschließlich eines konfliktgeschützten Hashes über die native Pods-API.', '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}$' ),
				'record_id' => array( 'type' => 'integer', 'minimum' => 1 ),
			),
			'required' => array( 'model_name', 'record_id' ), 'additionalProperties' => false,
		),
		'output_schema'       => wpagent_pods_act_record_output_schema(),
		'execute_callback'    => 'wpagent_exec_get_pods_act_record',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_list_pods_act_records( $input ) {
	$input = is_array( $input ) ? $input : array();
	$context = wpagent_pods_act_context( $input['model_name'] ?? null );
	if ( is_wp_error( $context ) ) {
		return $context;
	}
	$page = wpagent_pods_act_page_value( $input['page'] ?? null, 'page', 1, WPAGENT_PODS_ACT_MAX_PAGE );
	$per_page = wpagent_pods_act_page_value( $input['per_page'] ?? null, 'per_page', 20, WPAGENT_PODS_ACT_MAX_PER_PAGE );
	if ( is_wp_error( $page ) || is_wp_error( $per_page ) ) {
		return is_wp_error( $page ) ? $page : $per_page;
	}
	try {
		$context['native']->find( array( 'limit' => $per_page, 'page' => $page, 'orderby' => 'id ASC' ) );
	} catch ( Throwable $error ) {
		return new WP_Error( 'wpagent_pods_act_list_failed', __( 'Die Advanced-Content-Type-Datensätze konnten nicht gelesen werden.', 'wp-agent-companion' ), array( 'status' => 500 ) );
	}
	$records = array();
	try {
		while ( count( $records ) < $per_page && $context['native']->fetch() ) {
			$record = wpagent_pods_act_current_record( $context );
			if ( is_wp_error( $record ) ) {
				return $record;
			}
			$records[] = $record;
		}
		$total = max( 0, (int) $context['native']->total_found() );
	} catch ( Throwable $error ) {
		return new WP_Error( 'wpagent_pods_act_list_failed', __( 'Die Advanced-Content-Type-Datensätze konnten nicht vollständig gelesen werden.', 'wp-agent-companion' ), array( 'status' => 500 ) );
	}
	return array(
		'model_name' => $context['name'], 'blog_id' => $context['blog_id'], 'schema_hash' => $context['schema_hash'], 'fields' => $context['fields'],
		'records' => $records, 'total' => $total, 'page' => $page, 'per_page' => $per_page,
		'total_pages' => 0 === $total ? 0 : (int) ceil( $total / $per_page ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_pods_act_record( $input ) {
	$input = is_array( $input ) ? $input : array();
	$context = wpagent_pods_act_context( $input['model_name'] ?? null );
	if ( is_wp_error( $context ) ) {
		return $context;
	}
	$record_id = wpagent_pods_act_record_id( $input['record_id'] ?? null );
	return is_wp_error( $record_id ) ? $record_id : wpagent_pods_act_get_record( $context, $record_id );
}
