<?php
/** Ability: wp-agent/get-seopress-post-redirection. */

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

/** @return array<string,mixed> */
function wpagent_seopress_post_redirection_schema(): array {
	return array(
		'type'                 => 'object',
		'properties'           => array(
			'post_id'     => array( 'type' => 'integer', 'minimum' => 1 ),
			'provider'    => array( 'type' => 'string', 'enum' => array( 'seopress' ) ),
			'source'      => array( 'type' => 'string', 'format' => 'uri' ),
			'destination' => array( 'type' => 'string' ),
			'http_code'   => array( 'type' => 'integer', 'enum' => array( 301, 302, 307 ) ),
			'enabled'     => array( 'type' => 'boolean' ),
			'state_hash'  => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
		'required'             => array( 'post_id', 'provider', 'source', 'destination', 'http_code', 'enabled', 'state_hash' ),
		'additionalProperties' => false,
	);
}

/** @param array<string,mixed> $state @return array<string,mixed> */
function wpagent_seopress_post_redirection_public_state( array $state ): array {
	return array(
		'post_id'     => (int) $state['post_id'],
		'provider'    => 'seopress',
		'source'      => (string) $state['source'],
		'destination' => (string) $state['destination'],
		'http_code'   => (int) $state['http_code'],
		'enabled'     => (bool) $state['enabled'],
		'state_hash'  => (string) $state['state_hash'],
	);
}

function wpagent_ability_get_seopress_post_redirection(): array {
	return array(
		'label'               => __( 'SEOPress-Beitragsweiterleitung lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest eine einfache SEOPress-Free-Weiterleitung eines veröffentlichten Beitrags über SEOPress’ native REST-Schnittstelle. Die Quelle stammt stets aus der aktuellen Beitrags-URL.', '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'       => wpagent_seopress_post_redirection_schema(),
		'execute_callback'    => 'wpagent_exec_get_seopress_post_redirection',
		'permission_callback' => 'wpagent_perm_edit_seopress_post_redirection',
		'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_seopress_post_redirection( $input ) {
	$runtime = wpagent_seopress_post_redirection_runtime();
	if ( is_wp_error( $runtime ) ) {
		return $runtime;
	}
	$post_id = is_array( $input ) ? absint( $input['post_id'] ?? 0 ) : 0;
	$target  = wpagent_seopress_post_redirection_target( $post_id );
	if ( is_wp_error( $target ) ) {
		return $target;
	}
	$record = wpagent_seopress_post_redirection_read( $post_id, $target['source'] );
	return is_wp_error( $record ) ? $record : wpagent_seopress_post_redirection_public_state( $record['state'] );
}
