<?php
/** Fluent Forms discovery and read abilities. */

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

function wpagent_ability_list_fluent_forms(): array {
	return array(
		'label' => __( 'Fluent-Forms-Formulare auflisten', 'wp-agent-companion' ),
		'description' => __( 'Listet Fluent-Forms-Formulare über die native Plugin-API mit Suche, Status und Pagination.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object', 'additionalProperties' => false,
			'properties' => array(
				'search' => array( 'type' => 'string', 'maxLength' => 255 ),
				'status' => array( 'type' => 'string', 'enum' => array( 'published', 'unpublished', 'all' ), 'default' => 'all' ),
				'page' => array( 'type' => 'integer', 'minimum' => 1, 'default' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 20 ),
			),
		),
		'output_schema' => array( 'type' => 'object', 'properties' => array(
			'forms' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ) ),
			'total' => array( 'type' => 'integer' ), 'page' => array( 'type' => 'integer' ),
			'per_page' => array( 'type' => 'integer' ), 'last_page' => array( 'type' => 'integer' ),
		) ),
		'execute_callback' => 'wpagent_exec_list_fluent_forms',
		'permission_callback' => 'wpagent_perm_fluent_forms_list',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @param mixed $input @return array|WP_Error */
function wpagent_exec_list_fluent_forms( $input ) {
	$service = wpagent_fluent_forms_service();
	if ( is_wp_error( $service ) ) {
		return $service;
	}
	$input    = is_array( $input ) ? $input : array();
	$page     = max( 1, absint( $input['page'] ?? 1 ) );
	$per_page = min( 100, max( 1, absint( $input['per_page'] ?? 20 ) ) );
	try {
		$result = $service->get(
			array(
				'search' => sanitize_text_field( (string) ( $input['search'] ?? '' ) ),
				'status' => sanitize_key( (string) ( $input['status'] ?? 'all' ) ),
				'page' => $page, 'per_page' => $per_page, 'sort_column' => 'id', 'sort_by' => 'DESC',
			)
		);
	} catch ( Throwable $error ) {
		return new WP_Error( 'wpagent_fluent_forms_list_failed', __( 'Die Fluent-Forms-Formulare konnten nicht gelesen werden.', 'wp-agent-companion' ), array( 'status' => 500 ) );
	}
	$result = is_array( $result ) ? $result : array();
	$rows   = array();
	foreach ( is_array( $result['data'] ?? null ) ? $result['data'] : array() as $form ) {
		$rows[] = wpagent_fluent_forms_public_form_payload( $form );
	}
	return array(
		'forms' => $rows, 'total' => absint( $result['total'] ?? count( $rows ) ),
		'page' => absint( $result['current_page'] ?? $page ), 'per_page' => absint( $result['per_page'] ?? $per_page ),
		'last_page' => absint( $result['last_page'] ?? ( $rows ? 1 : 0 ) ),
	);
}

function wpagent_ability_get_fluent_form(): array {
	return array(
		'label' => __( 'Fluent-Forms-Formular lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest ein Fluent-Forms-Formular über die native Plugin-API. Unbekannte Add-on- und Form-Meta-Daten werden nicht ausgegeben.', '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_form_output_schema(),
		'execute_callback' => 'wpagent_exec_get_fluent_form',
		'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( $input ) {
	$authorized = wpagent_fluent_forms_get_authorized_form( $input );
	return is_wp_error( $authorized ) ? $authorized : wpagent_fluent_forms_public_form_payload( $authorized['form'] );
}
