<?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' => wpagent_forms_public_object_schema( array(
			'forms' => wpagent_forms_public_list_schema( wpagent_fluent_forms_form_output_schema(), WPAGENT_FORMS_PUBLIC_MAX_FORMS ),
			'total' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 1000000 ), 'page' => array( 'type' => 'integer', 'minimum' => 1 ),
			'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => WPAGENT_FORMS_PUBLIC_MAX_FORMS ), 'last_page' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 1000000 ),
		), array( 'forms', 'total', 'page', 'per_page', 'last_page' ) ),
		'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();
	$data = $result['data'] ?? array();
	if ( ! is_array( $data ) || ! array_is_list( $data ) || count( $data ) > WPAGENT_FORMS_PUBLIC_MAX_FORMS ) {
		return wpagent_forms_public_output_error( 'fluent_forms', 'forms' );
	}
	$rows   = array();
	foreach ( $data as $summary ) {
		$summary_data = wpagent_fluent_forms_model_array( $summary );
		$form_id      = absint( $summary_data['id'] ?? 0 );
		if ( $form_id <= 0 ) {
			return wpagent_forms_public_output_error( 'fluent_forms', 'id' );
		}
		try {
			$form = $service->find( $form_id );
		} catch ( Throwable $error ) {
			return new WP_Error( 'wpagent_fluent_forms_list_failed', __( 'Ein aufgelistetes Fluent-Forms-Formular konnte nicht vollständig gelesen werden.', 'wp-agent-companion' ), array( 'status' => 500 ) );
		}
		$payload = wpagent_fluent_forms_public_form_payload( $form );
		if ( is_wp_error( $payload ) ) {
			return $payload;
		}
		$rows[] = $payload;
	}
	$total = absint( $result['total'] ?? count( $rows ) );
	$last_page = absint( $result['last_page'] ?? ( $rows ? 1 : 0 ) );
	if ( $total > 1000000 || $last_page > 1000000 ) {
		return wpagent_forms_public_output_error( 'fluent_forms', 'pagination' );
	}
	return array(
		'forms' => $rows, 'total' => $total,
		'page' => absint( $result['current_page'] ?? $page ), 'per_page' => absint( $result['per_page'] ?? $per_page ),
		'last_page' => $last_page,
	);
}

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'] );
}
