<?php
/** Verification helpers for portable form migration. */

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

/** @param array<int,array<string,mixed>> $fields @return array<int,array<string,mixed>> */
function wpagent_form_migration_comparable_fields( array $fields ): array {
	return array_map(
		static fn( array $field ): array => array(
			'type'        => (string) ( $field['type'] ?? '' ),
			'label'       => (string) ( $field['label'] ?? '' ),
			'description' => (string) ( $field['description'] ?? '' ),
			'placeholder' => (string) ( $field['placeholder'] ?? '' ),
			'default'     => (string) ( $field['default'] ?? '' ),
			'required'    => ! empty( $field['required'] ),
			'choices'     => is_array( $field['choices'] ?? null ) ? $field['choices'] : array(),
		),
		$fields
	);
}

/** @param string[] $expected_types @param string[] $actual_types */
function wpagent_form_migration_verification_error( bool $removed, string $actual_error, array $expected_types, array $actual_types, string $target_provider, int $target_form_id ): WP_Error {
	$code    = $removed ? 'wpagent_form_migration_unverified' : 'wpagent_form_migration_rollback_failed';
	$message = $removed
		? __( 'Die Zielkopie wich vom portablen Formular ab und wurde entfernt.', 'wp-agent-companion' )
		: __( 'Die Zielkopie wich vom portablen Formular ab und konnte nicht vollständig entfernt werden. Prüfe sie manuell.', 'wp-agent-companion' );
	return new WP_Error(
		$code,
		$message,
		array(
			'status'          => 500,
			'target_removed'  => $removed,
			'target_provider' => $target_provider,
			'target_form_id'  => $target_form_id,
			'actual_error'    => $actual_error,
			'expected_types'  => $expected_types,
			'actual_types'    => $actual_types,
		)
	);
}
