<?php
/** Safe WPForms confirmation and notification read ability. */

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

/** @return array<string,mixed> */
function wpagent_wpforms_confirmation_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'id' => array( 'type' => 'integer' ), 'name' => array( 'type' => 'string' ),
			'type' => array( 'type' => 'string', 'enum' => array( 'message', 'page', 'redirect' ) ),
			'message' => array( 'type' => 'string' ), 'message_scroll' => array( 'type' => 'boolean' ),
			'page_id' => array( 'type' => 'integer' ), 'page_url_parameters' => array( 'type' => 'string' ),
			'redirect' => array( 'type' => 'string' ), 'redirect_new_tab' => array( 'type' => 'boolean' ),
			'hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
	);
}

/** @return array<string,mixed> */
function wpagent_wpforms_notification_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'id' => array( 'type' => 'integer' ), 'name' => array( 'type' => 'string' ), 'subject' => array( 'type' => 'string' ),
			'email' => array( 'type' => 'string' ), 'sender_name' => array( 'type' => 'string' ),
			'sender_address' => array( 'type' => 'string' ), 'reply_to' => array( 'type' => 'string' ),
			'carbon_copy' => array( 'type' => 'string' ), 'message' => array( 'type' => 'string' ),
			'hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
	);
}

/** @return array<string,mixed> */
function wpagent_wpforms_delivery_output_schema(): array {
	return array(
		'type' => 'object',
		'properties' => array(
			'form_id' => array( 'type' => 'integer' ),
			'notifications_enabled' => array( 'type' => 'boolean' ),
			'confirmations' => array( 'type' => 'array', 'items' => wpagent_wpforms_confirmation_schema() ),
			'notifications' => array( 'type' => 'array', 'items' => wpagent_wpforms_notification_schema() ),
			'state_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			'notification_details_included' => array( 'type' => 'boolean' ),
		),
	);
}

function wpagent_ability_get_wpforms_delivery(): array {
	return array(
		'label' => __( 'WPForms-Zustellung lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest Bestätigungen und kompakte E-Mail-Benachrichtigungen eines WPForms-Formulars. Adressen und Nachrichtentexte erscheinen nur nach ausdrücklichem Opt-in. Unbekannte Add-on-Daten fließen in den Konflikt-Hash ein, werden aber nicht ausgegeben.', '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_wpforms_delivery_output_schema(),
		'execute_callback' => 'wpagent_exec_get_wpforms_delivery',
		'permission_callback' => 'wpagent_perm_wpforms_delivery',
		'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_wpforms_delivery( $input ) {
	$input = is_array( $input ) ? $input : array();
	$state = wpagent_wpforms_delivery_state( absint( $input['form_id'] ?? 0 ) );
	return is_wp_error( $state ) ? $state : wpagent_wpforms_delivery_public( $state, true === ( $input['include_notification_details'] ?? false ) );
}
