<?php
/** Read-only abilities for persistent ACF field groups. */

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

function wpagent_ability_list_acf_field_groups(): array {
	return array(
		'label' => __( 'ACF-Feldgruppen auflisten', 'wp-agent-companion' ),
		'description' => __( 'Listet persistente ACF-Feldgruppen mit Inhaltstypen, Feldzahl, Portabilität und Konflikt-Hash. Lokale PHP- und JSON-Gruppen werden nicht als veränderbar ausgegeben.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object',
			'properties' => array(
				'search' => array( 'type' => 'string', 'maxLength' => 100 ),
				'page' => array( 'type' => 'integer', 'minimum' => 1, 'default' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50, 'default' => 20 ),
			),
		),
		'output_schema' => array(
			'type' => 'object',
			'properties' => array(
				'groups' => array( 'type' => 'array', 'items' => wpagent_acf_group_output_schema() ),
				'total' => array( 'type' => 'integer' ),
				'page' => array( 'type' => 'integer' ),
				'per_page' => array( 'type' => 'integer' ),
				'total_pages' => array( 'type' => 'integer' ),
			),
		),
		'execute_callback' => 'wpagent_exec_list_acf_field_groups',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

function wpagent_ability_get_acf_field_group(): array {
	return array(
		'label' => __( 'ACF-Feldgruppe lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest eine persistente ACF-Feldgruppe einschließlich ihrer begrenzten Felddefinitionen und eines Konflikt-Hashs über die native ACF-API.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object',
			'properties' => array( 'group_key' => array( 'type' => 'string', 'pattern' => '^group_[A-Za-z0-9_-]{1,120}$' ) ),
			'required' => array( 'group_key' ),
		),
		'output_schema' => wpagent_acf_group_output_schema(),
		'execute_callback' => 'wpagent_exec_get_acf_field_group',
		'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_acf_field_groups( $input ) {
	$available = wpagent_acf_group_require_api();
	if ( is_wp_error( $available ) ) {
		return $available;
	}
	$input = is_array( $input ) ? $input : array();
	$search = is_string( $input['search'] ?? null ) ? trim( $input['search'] ) : '';
	if ( wpagent_acf_group_text_length( $search ) > 100 ) {
		return new WP_Error( 'wpagent_acf_group_search', __( 'search darf höchstens 100 Zeichen enthalten.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	$page = max( 1, absint( $input['page'] ?? 1 ) );
	$per_page = max( 1, min( 50, absint( $input['per_page'] ?? 20 ) ) );
	$groups = acf_get_raw_field_groups();
	$groups = is_array( $groups ) ? $groups : array();
	$groups = array_values(
		array_filter(
			$groups,
			static function ( $group ) use ( $search ): bool {
				if ( ! is_array( $group ) || absint( $group['ID'] ?? 0 ) < 1 || ! empty( $group['local'] ) || 'acf-field-group' !== get_post_type( absint( $group['ID'] ?? 0 ) ) ) {
					return false;
				}
				return '' === $search || false !== stripos( (string) ( $group['title'] ?? '' ), $search ) || false !== stripos( (string) ( $group['key'] ?? '' ), $search );
			}
		)
	);
	usort(
		$groups,
		static fn( array $left, array $right ): int => strnatcasecmp( (string) ( $left['title'] ?? '' ), (string) ( $right['title'] ?? '' ) ) ?: strcmp( (string) ( $left['key'] ?? '' ), (string) ( $right['key'] ?? '' ) )
	);
	$total = count( $groups );
	$groups = array_slice( $groups, ( $page - 1 ) * $per_page, $per_page );
	return array(
		'groups' => array_map( 'wpagent_acf_group_state', $groups ),
		'total' => $total,
		'page' => $page,
		'per_page' => $per_page,
		'total_pages' => $total > 0 ? (int) ceil( $total / $per_page ) : 0,
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_acf_field_group( $input ) {
	$available = wpagent_acf_group_require_api();
	if ( is_wp_error( $available ) ) {
		return $available;
	}
	$input = is_array( $input ) ? $input : array();
	$group = wpagent_acf_group_get_persistent( is_string( $input['group_key'] ?? null ) ? $input['group_key'] : '' );
	return is_wp_error( $group ) ? $group : wpagent_acf_group_state( $group );
}
