<?php
/** Read verified Ocean Extra per-post header, footer, sidebar and width overrides. */

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

function wpagent_ability_get_oceanwp_post_layout_overrides(): array {
	return array(
		'label'               => __( 'Ocean-Extra-Beitragslayout lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest die sicheren Ocean-Extra-Header-, Footer-, Sidebar- und Breitenvorgaben 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' ),
				'overrides'  => wpagent_oceanwp_extra_layout_state_schema(),
				'state_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			),
			'required'             => array( 'post_id', 'post_type', 'overrides', 'state_hash' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_get_oceanwp_post_layout_overrides',
		'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_layout_overrides( $input ) {
	$ready = wpagent_oceanwp_extra_layout_require_runtime();
	if ( is_wp_error( $ready ) ) {
		return $ready;
	}
	$target = wpagent_oceanwp_extra_layout_post_target( absint( is_array( $input ) ? ( $input['post_id'] ?? 0 ) : 0 ) );
	if ( is_wp_error( $target ) ) {
		return $target;
	}
	$snapshot = wpagent_oceanwp_extra_layout_snapshot( $target['post'], $target['blog_id'] );
	if ( is_wp_error( $snapshot ) ) {
		return $snapshot;
	}
	return array(
		'post_id'    => (int) $target['post']->ID,
		'post_type'  => $target['post']->post_type,
		'overrides'  => wpagent_oceanwp_extra_layout_normalize( $snapshot ),
		'state_hash' => wpagent_oceanwp_extra_layout_state_hash( $snapshot ),
	);
}
