<?php
/**
 * Ability: wp-agent/get-seo-meta
 *
 * @package WPAgentCompanion
 */

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

function wpagent_ability_get_seo_meta(): array {
	$state_hash = array(
		'type'      => 'string',
		'minLength' => 64,
		'maxLength' => 64,
		'pattern'   => '^[a-f0-9]{64}$',
	);
	$output_properties = array(
		'post_id'                  => array( 'type' => 'integer', 'minimum' => 1 ),
		'plugin'                   => array( 'type' => 'string', 'enum' => array( 'rankmath', 'yoast', 'aioseo', 'seopress' ) ),
		'seo_title'                => wpagent_seo_text_input_schema( 'title' ),
		'seo_description'          => wpagent_seo_text_input_schema( 'description' ),
		'focus_keyword'            => wpagent_seo_text_input_schema( 'focus_keyword' ),
		'canonical_url'            => wpagent_seo_text_input_schema( 'canonical_url' ),
		'robots_noindex'           => array( 'type' => 'boolean' ),
		'robots_nofollow'          => array( 'type' => 'boolean' ),
		'open_graph_title'         => wpagent_seo_text_input_schema( 'open_graph_title' ),
		'open_graph_description'   => wpagent_seo_text_input_schema( 'open_graph_description' ),
		'twitter_title'            => wpagent_seo_text_input_schema( 'twitter_title' ),
		'twitter_description'      => wpagent_seo_text_input_schema( 'twitter_description' ),
		'cornerstone_supported'    => array( 'type' => 'boolean' ),
		'cornerstone'              => array( 'type' => array( 'boolean', 'null' ) ),
		'state_hash'               => $state_hash,
	);
	return array(
		'label'               => __( 'SEO-Meta lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest die gespeicherten SEO-, Robots-, Social- und Cornerstone-Meta des aktiven unterstützten SEO-Plugins in einem einheitlichen Format.', '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'           => $output_properties,
			'required'             => array_keys( $output_properties ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_get_seo_meta',
		'permission_callback' => 'wpagent_perm_edit_post_target',
		'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_seo_meta( $input ) {
	$input   = is_array( $input ) ? $input : array();
	$post_id = (int) ( $input['post_id'] ?? 0 );
	// Authorize the target before resolving it. This preserves one 403 result
	// for an inaccessible post and an absent ID when a callback is invoked
	// directly instead of through the Ability/REST permission boundary.
	if ( $post_id <= 0 || ! current_user_can( 'edit_post', $post_id ) ) {
		return new WP_Error( 'wpagent_forbidden', __( 'Keine Berechtigung.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	if ( ! get_post( $post_id ) ) {
		return new WP_Error( 'wpagent_no_post', __( 'Beitrag nicht gefunden.', 'wp-agent-companion' ), array( 'status' => 404 ) );
	}
	$plugin = wpagent_detect_seo_plugin();
	if ( 'none' === $plugin ) {
		return new WP_Error( 'wpagent_seo_plugin_missing', __( 'Kein unterstütztes SEO-Plugin aktiv.', 'wp-agent-companion' ), array( 'status' => 409 ) );
	}
	return wpagent_read_seo_meta( $post_id, $plugin );
}
