<?php
/** Fluent Forms confirmation and email-notification read ability. */

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

/** @return array<string,mixed> */
function wpagent_fluent_forms_confirmation_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'redirect_to' => array( 'type' => 'string', 'enum' => array( 'same_page', 'custom_url', 'custom_page' ) ),
			'message' => array( 'type' => 'string' ),
			'same_page_behavior' => array( 'type' => 'string', 'enum' => array( 'hide_form', 'reset_form' ) ),
			'custom_page_id' => array( 'type' => 'integer' ),
			'custom_url' => array( 'type' => 'string' ),
			'redirect_message' => array( 'type' => 'string' ),
			'enable_query_string' => array( 'type' => 'boolean' ),
			'query_strings' => array( 'type' => 'string' ),
		),
	);
}

/** @return array<string,mixed> */
function wpagent_fluent_forms_send_to_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'type' => array( 'type' => 'string', 'enum' => array( 'email', 'field' ) ),
			'email' => array( 'type' => 'string' ),
			'field' => array( 'type' => 'string' ),
		),
	);
}

/** @return array<string,mixed> */
function wpagent_fluent_forms_notification_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'id' => array( 'type' => 'integer' ), 'name' => array( 'type' => 'string' ),
			'enabled' => array( 'type' => 'boolean' ), 'send_to' => wpagent_fluent_forms_send_to_schema(),
			'from_name' => array( 'type' => 'string' ), 'from_email' => array( 'type' => 'string' ),
			'reply_to' => array( 'type' => 'string' ), 'bcc' => array( 'type' => 'string' ),
			'subject' => array( 'type' => 'string' ), 'message' => array( 'type' => 'string' ),
			'email_template' => array( 'type' => 'string' ),
			'hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
	);
}

/** @return array<string,mixed> */
function wpagent_fluent_forms_delivery_output_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'form_id' => array( 'type' => 'integer' ),
			'confirmation' => wpagent_fluent_forms_confirmation_schema(),
			'confirmation_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			'notifications' => array( 'type' => 'array', 'items' => wpagent_fluent_forms_notification_schema() ),
			'notifications_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			'notification_details_included' => array( 'type' => 'boolean' ),
		),
	);
}

function wpagent_ability_get_fluent_form_delivery(): array {
	return array(
		'label' => __( 'Fluent-Forms-Zustellung lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest die Bestätigung und kompakte E-Mail-Benachrichtigungen eines Fluent-Forms-Formulars. Adressen und Nachrichtentexte werden nur nach ausdrücklichem Opt-in ausgegeben. Konflikt-Hashes schützen spätere Einzeländerungen. Unbekannte Add-on-Daten bleiben verborgen.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object', 'additionalProperties' => false,
			'properties' => array(
				'form_id' => array( 'type' => 'integer', 'minimum' => 1 ),
				'include_notification_details' => array( 'type' => 'boolean', 'default' => false ),
			),
			'required' => array( 'form_id' ),
		),
		'output_schema' => wpagent_fluent_forms_delivery_output_schema(),
		'execute_callback' => 'wpagent_exec_get_fluent_form_delivery',
		'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<string,mixed>|WP_Error */
function wpagent_exec_get_fluent_form_delivery( $input ) {
	$input    = is_array( $input ) ? $input : array();
	$form_id  = absint( $input['form_id'] ?? 0 );
	$details  = true === ( $input['include_notification_details'] ?? false );
	$state    = wpagent_fluent_forms_delivery_state( $form_id );
	return is_wp_error( $state ) ? $state : wpagent_fluent_forms_delivery_public( $state, $details );
}
