<?php
/**
 * Shared post SEO readback verification.
 *
 * @package WPAgentCompanion
 */

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

/**
 * @param array<string,string|bool|null> $fields
 * @param array<string,mixed>            $snapshot
 * @return array{written:array<string,array<string,mixed>>,verified:bool}
 */
function wpagent_verify_seo_snapshot( array $fields, array $snapshot ): array {
	$snapshot_keys = array(
		'title'                   => 'seo_title',
		'description'             => 'seo_description',
		'focus_keyword'           => 'focus_keyword',
		'canonical_url'           => 'canonical_url',
		'robots_noindex'          => 'robots_noindex',
		'robots_nofollow'         => 'robots_nofollow',
		'open_graph_title'        => 'open_graph_title',
		'open_graph_description'  => 'open_graph_description',
		'twitter_title'           => 'twitter_title',
		'twitter_description'     => 'twitter_description',
		'cornerstone'             => 'cornerstone',
	);
	$written      = array();
	$verified     = true;
	foreach ( $snapshot_keys as $field => $snapshot_key ) {
		if ( null === ( $fields[ $field ] ?? null ) ) {
			continue;
		}
		$expected = $fields[ $field ];
		$actual   = $snapshot[ $snapshot_key ] ?? null;
		$ok       = is_bool( $expected ) ? $actual === $expected : (string) $actual === (string) $expected;
		$written[ $field ] = array( 'persisted' => $ok );
		$verified          = $verified && $ok;
	}
	return array( 'written' => $written, 'verified' => $verified );
}
