<?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' => 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' ),
		) ),
		'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 );
	$forms = array_slice( $forms, ( $page - 1 ) * $per_page, $per_page );
	$rows  = array();
	foreach ( $forms as $form ) {
		if ( ! is_array( $form ) ) {
			continue;
		}
		$payload = wpagent_gravity_form_payload( $form );
		$rows[]  = array_intersect_key( $payload, array_flip( array( 'provider', 'id', 'title', 'date_created', 'is_active', 'is_trash', 'shortcode' ) ) );
	}
	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 );
}
