<?php
/** Read-only Fluent Forms field surface. */

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

/** @return array<string,mixed> */
function wpagent_fluent_forms_field_type_output_schema(): array {
	return array(
		'type'       => 'object',
		'properties' => array(
			'type' => array( 'type' => 'string' ), 'available' => array( 'type' => 'boolean' ),
			'reason' => array( 'type' => 'string' ),
			'supports' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
		),
	);
}

/** @return array<string,mixed> */
function wpagent_fluent_forms_public_field_output_schema(): array {
	return array(
		'type'       => 'object',
		'properties' => array(
			'key' => array( 'type' => 'string' ), 'type' => array( 'type' => 'string' ),
			'provider_element' => array( 'type' => 'string' ), 'label' => array( 'type' => 'string' ),
			'help_message' => array( 'type' => 'string' ), 'placeholder' => array( 'type' => 'string' ),
			'required' => array( 'type' => 'boolean' ), 'default_value' => array( 'type' => 'string' ),
			'default_values' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
			'max_length' => array( 'type' => 'integer' ),
			'min' => array( 'type' => 'number' ), 'max' => array( 'type' => 'number' ),
			'step' => array( 'type' => 'number' ),
			'choices' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ) ),
			'subfields' => array( 'type' => 'object' ),
			'date_format' => array( 'type' => 'string' ), 'time_enabled' => array( 'type' => 'boolean' ),
		),
	);
}

/** @return array<string,mixed> */
function wpagent_fluent_forms_fields_output_schema(): array {
	return array(
		'type'       => 'object',
		'properties' => array(
			'form_id' => array( 'type' => 'integer' ),
			'fields' => array( 'type' => 'array', 'items' => wpagent_fluent_forms_public_field_output_schema() ),
			'field_count' => array( 'type' => 'integer' ), 'fields_hash' => array( 'type' => 'string' ),
			'empty' => array( 'type' => 'boolean' ), 'submit_label' => array( 'type' => 'string' ),
			'field_types' => array( 'type' => 'array', 'items' => wpagent_fluent_forms_field_type_output_schema() ),
			'verified' => array( 'type' => 'boolean' ), 'deleted' => array( 'type' => 'boolean' ),
			'deleted_field_key' => array( 'type' => 'string' ),
		),
	);
}

function wpagent_ability_get_fluent_form_fields(): array {
	return array(
		'label' => __( 'Fluent-Forms-Felder sicher lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest eine kompakte, geheimnisfreie Feldstruktur, den vollständigen Konflikt-Hash und die tatsächlich verfügbaren nativen Feldtypen.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object', 'additionalProperties' => false,
			'properties' => array( 'form_id' => array( 'type' => 'integer', 'minimum' => 1 ) ),
			'required' => array( 'form_id' ),
		),
		'output_schema' => wpagent_fluent_forms_fields_output_schema(),
		'execute_callback' => 'wpagent_exec_get_fluent_form_fields',
		'permission_callback' => 'wpagent_perm_fluent_forms_manage',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @param mixed $input @return array|WP_Error */
function wpagent_exec_get_fluent_form_fields( $input ) {
	$authorized = wpagent_fluent_forms_get_authorized_form( $input );
	return is_wp_error( $authorized ) ? $authorized : wpagent_fluent_forms_fields_state( $authorized );
}
