<?php
/** Ability: wp-agent/get-seo-settings. */

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

function wpagent_ability_get_seo_settings(): array {
	return array(
		'label'               => __( 'Globale SEO-Einstellungen lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest globale Titelvorlagen, Archivregeln und Markenidentität aus Rank Math, Yoast SEO, AIOSEO oder SEOPress in einem einheitlichen, konfliktgeschützten Format.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array(
			'type'                 => 'object',
			'properties'           => array(),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'       => 'object',
			'properties' => array(
				'plugin'                   => array( 'type' => 'string' ),
				'supported_fields'         => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
				'unmapped_template_fields' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
				'template_syntax'          => array( 'type' => 'string' ),
				'template_tokens'          => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
				'homepage_source'          => array( 'type' => 'string', 'enum' => array( 'global', 'static_page' ) ),
				'settings'                 => array( 'type' => 'object' ),
				'state_hash'               => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			),
		),
		'execute_callback'    => 'wpagent_exec_get_seo_settings',
		'permission_callback' => 'wpagent_perm_manage_options',
		'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_settings( $input ) {
	unset( $input );
	if ( ! current_user_can( 'manage_options' ) ) {
		return new WP_Error( 'wpagent_forbidden', __( 'Keine Berechtigung zum Lesen globaler SEO-Einstellungen.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	$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_settings( $plugin );
}
