<?php
/** Read-only Astra design inventory with provider-backed current values. */

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

function wpagent_ability_get_astra_design(): array {
	return array(
		'label'               => __( 'Astra-Design lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest 21 sichere Astra-Designbereiche samt strikten Eingabeschemata und Zustands-Hash.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => false, 'default' => array() ),
		'output_schema'       => array(
			'type'                 => 'object',
			'properties'           => array(
				'active'            => array( 'type' => 'boolean' ),
				'version'           => array( 'type' => 'string' ),
				'abilities_enabled' => array( 'type' => 'boolean' ),
				'operations'        => array(
					'type'  => 'array',
					'items' => array(
						'type'                 => 'object',
						'properties'           => array(
							'id'           => array( 'type' => 'string', 'enum' => array_keys( wpagent_astra_operation_catalog() ) ),
							'label'        => array( 'type' => 'string' ),
							'input_schema' => array( 'type' => 'object', 'additionalProperties' => true ),
							'current'      => array( 'type' => 'object', 'additionalProperties' => true ),
						),
						'required'             => array( 'id', 'label', 'input_schema', 'current' ),
						'additionalProperties' => false,
					),
				),
				'state_hash'        => array( 'type' => array( 'string', 'null' ), 'pattern' => '^[a-f0-9]{64}$' ),
			),
			'required'             => array( 'active', 'version', 'abilities_enabled', 'operations', 'state_hash' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_get_astra_design',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta'                => array(
			'show_in_rest' => true,
			'annotations'  => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ),
			'mcp'          => array(
				'uri'         => 'wpagently://integrations/astra',
				'annotations' => array( 'audience' => array( 'assistant' ) ),
			),
		),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_astra_design( $input = array() ) {
	$active  = wpagent_astra_is_active();
	$version = wpagent_astra_version();
	if ( ! $active ) {
		return array( 'active' => false, 'version' => $version, 'abilities_enabled' => false, 'operations' => array(), 'state_hash' => null );
	}

	$snapshot = wpagent_astra_snapshot();
	if ( is_wp_error( $snapshot ) ) {
		return $snapshot;
	}
	$enabled    = wpagent_astra_abilities_enabled();
	$operations = array();
	foreach ( wpagent_astra_available_operations() as $id => $definition ) {
		$current = wpagent_astra_provider_data( $definition['read'] );
		if ( is_wp_error( $current ) ) {
			return $current;
		}
		$operations[] = array(
			'id'           => $id,
			'label'        => $definition['label'],
			'input_schema' => $definition['input_schema'],
			'current'      => $current,
		);
	}
	return array(
		'active'            => true,
		'version'           => $version,
		'abilities_enabled' => $enabled,
		'operations'        => $operations,
		'state_hash'        => wpagent_astra_state_hash( $snapshot ),
	);
}
