<?php
/** Read verified Ocean Extra per-post page-title overrides. */

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

function wpagent_ability_get_oceanwp_post_title(): array {
	return array(
		'label'               => __( 'Ocean-Extra-Beitragstitel lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest die sicheren Ocean-Extra-Seitentitelvorgaben eines Beitrags samt Konflikthash.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array(
			'type'                 => 'object',
			'properties'           => array( 'post_id' => array( 'type' => 'integer', 'minimum' => 1 ) ),
			'required'             => array( 'post_id' ),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'                 => 'object',
			'properties'           => array(
				'post_id'    => array( 'type' => 'integer' ),
				'post_type'  => array( 'type' => 'string' ),
				'title'      => wpagent_oceanwp_extra_title_state_schema(),
				'state_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			),
			'required'             => array( 'post_id', 'post_type', 'title', 'state_hash' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_get_oceanwp_post_title',
		'permission_callback' => 'wpagent_perm_edit_posts',
		'meta'                => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_oceanwp_post_title( $input ) {
	$ready = wpagent_oceanwp_extra_title_require_runtime();
	if ( is_wp_error( $ready ) ) {
		return $ready;
	}
	$post = wpagent_oceanwp_post_target( absint( is_array( $input ) ? ( $input['post_id'] ?? 0 ) : 0 ) );
	if ( is_wp_error( $post ) ) {
		return $post;
	}
	$snapshot = wpagent_oceanwp_extra_title_snapshot( $post );
	if ( is_wp_error( $snapshot ) ) {
		return $snapshot;
	}
	return array(
		'post_id'    => (int) $post->ID,
		'post_type'  => $post->post_type,
		'title'      => wpagent_oceanwp_extra_title_normalize( $snapshot ),
		'state_hash' => wpagent_oceanwp_extra_title_state_hash( $snapshot ),
	);
}
