<?php
/** Gravity Forms list and get abilities backed by the public GFAPI. */

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

function wpagent_ability_list_gravity_forms(): array {
	return array(
		'label' => __( 'Gravity-Forms-Formulare auflisten', 'wp-agent-companion' ),
		'description' => __( 'Listet Gravity-Forms-Formulare über die öffentliche GFAPI mit Status, Suche 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( 'active', 'inactive', 'trash', 'all' ), 'default' => 'active' ),
				'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_gravity_form_list_item_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 ),
		), array( 'forms', 'total', 'page', 'per_page' ) ),
		'execute_callback' => 'wpagent_exec_list_gravity_forms',
		'permission_callback' => 'wpagent_perm_gravity_read',
		'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_gravity_forms( $input ) {
	if ( ! wpagent_gravity_available() ) {
		return wpagent_gravity_missing_error();
	}
	if ( ! current_user_can( 'gravityforms_edit_forms' ) ) {
		return new WP_Error( 'wpagent_forbidden', __( 'Keine Berechtigung für Gravity Forms.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	$input    = is_array( $input ) ? $input : array();
	$status   = (string) ( $input['status'] ?? 'active' );
	$page     = max( 1, absint( $input['page'] ?? 1 ) );
	$per_page = min( 100, max( 1, absint( $input['per_page'] ?? 20 ) ) );
	$filters  = array(
		'active'   => array( true, false ),
		'inactive' => array( false, false ),
		'trash'    => array( null, true ),
		'all'      => array( null, null ),
	);
	list( $active, $trash ) = $filters[ $status ] ?? $filters['active'];
	$forms = GFAPI::get_forms( $active, $trash, 'id', 'DESC' );
	if ( is_wp_error( $forms ) ) {
		return $forms;
	}
	$forms = is_array( $forms ) ? $forms : array();
	$search = trim( (string) ( $input['search'] ?? '' ) );
	if ( '' !== $search ) {
		$forms = array_values( array_filter( $forms, static fn( $form ): bool => is_array( $form ) && false !== stripos( (string) ( $form['title'] ?? '' ), $search ) ) );
	}
	$total = count( $forms );
	if ( $total > 1000000 ) {
		return wpagent_forms_public_output_error( 'gravity', 'total' );
	}
	$forms = array_slice( $forms, ( $page - 1 ) * $per_page, $per_page );
	$rows  = array();
	foreach ( $forms as $form ) {
		if ( ! is_array( $form ) ) {
			return wpagent_forms_public_output_error( 'gravity', 'forms' );
		}
		$payload = wpagent_gravity_form_payload( $form );
		if ( is_wp_error( $payload ) ) {
			return $payload;
		}
		$rows[]  = array_intersect_key( $payload, array_flip( array( 'provider', 'id', 'title', 'date_created', 'is_active', 'is_trash', 'shortcode', 'state_hash' ) ) );
	}
	return array( 'forms' => $rows, 'total' => $total, 'page' => $page, 'per_page' => $per_page );
}

function wpagent_ability_get_gravity_form(): array {
	return array(
		'label' => __( 'Gravity-Forms-Formular lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest die dokumentierte Formular-, Feld-, Benachrichtigungs- und Bestätigungskonfiguration über die öffentliche GFAPI. Unbekannte Add-on-Daten auf Formularebene 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_gravity_form_output_schema(),
		'execute_callback' => 'wpagent_exec_get_gravity_form',
		'permission_callback' => 'wpagent_perm_gravity_read',
		'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_gravity_form( $input ) {
	$form = wpagent_gravity_get_authorized_form( $input, 'gravityforms_edit_forms' );
	return is_wp_error( $form ) ? $form : wpagent_gravity_form_payload( $form );
}
