<?php
/** Site Context lesen und verwalten. */

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

function wpagent_site_context_schema(): array {
	$properties = array();
	foreach ( array( 'site_name', 'audience', 'voice', 'goals', 'constraints', 'notes' ) as $field ) {
		$properties[ $field ] = array( 'type' => 'string' );
	}
	return array( 'type' => 'object', 'properties' => $properties );
}

/** @return array<string,int> */
function wpagent_site_context_limits(): array {
	return array( 'site_name' => 200, 'audience' => 2000, 'voice' => 2000, 'goals' => 3000, 'constraints' => 3000, 'notes' => 5000 );
}

/** @return array<string,string> */
function wpagent_normalized_site_context(): array {
	$stored  = wpagent_state_array_option( WPAGENT_COMPANION_OPT_SITE_CONTEXT );
	$context = array();
	foreach ( wpagent_site_context_limits() as $field => $limit ) {
		$value = wpagent_state_text( $stored[ $field ] ?? '', $limit, $field );
		$context[ $field ] = is_wp_error( $value ) ? '' : $value;
	}
	return $context;
}

function wpagent_ability_get_site_context(): array {
	return array(
		'label' => __( 'Site Context lesen', 'wp-agent-companion' ), 'description' => __( 'Liest den zentralen Kontext für Zielgruppe, Tonalität, Ziele und Einschränkungen.', 'wp-agent-companion' ), 'category' => 'wp-agent',
		'input_schema' => array( 'type' => 'object', 'properties' => array(), 'default' => array() ),
		'output_schema' => array( 'type' => 'object', 'properties' => array( 'context' => wpagent_site_context_schema(), 'agent_context' => wpagent_companion_chat_context_schema() ) ),
		'execute_callback' => 'wpagent_exec_get_site_context', 'permission_callback' => 'wpagent_perm_edit_posts',
		'meta' => array(
			'show_in_rest' => true,
			'annotations'  => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ),
			'mcp'          => array(
				'uri'         => 'wpagently://site/context',
				'annotations' => array( 'audience' => array( 'assistant' ) ),
			),
		),
	);
}

function wpagent_ability_update_site_context(): array {
	return array(
		'label' => __( 'Site Context aktualisieren', 'wp-agent-companion' ), 'description' => __( 'Speichert den zentralen, begrenzten Site Context für alle Agent-Läufe.', 'wp-agent-companion' ), 'category' => 'wp-agent',
		'input_schema' => wpagent_site_context_schema(),
		'output_schema' => array( 'type' => 'object', 'properties' => array( 'context' => wpagent_site_context_schema(), 'verified' => array( 'type' => 'boolean' ) ) ),
		'execute_callback' => 'wpagent_exec_update_site_context', 'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => false, 'destructive' => false, 'idempotent' => true ) ),
	);
}

function wpagent_exec_get_site_context( $input = array() ): array {
	// This authenticated MCP resource gives every compatible client the same
	// bounded base context as Chat. Prompt-specific memories remain absent
	// because a resource read has no trusted task prompt for relevance ranking.
	// Never bypass the assembler's redaction through the legacy context field.
	return array(
		'context'       => wpagent_companion_chat_context_site_data( array( 'site_name', 'audience', 'voice', 'goals', 'constraints', 'notes' ), true ),
		'agent_context' => wpagent_companion_chat_assemble_context(),
	);
}

/** @return array<string,string>|WP_Error */
function wpagent_validate_site_context( $input, ?array $base = null ) {
	$input   = is_array( $input ) ? $input : array();
	$limits  = wpagent_site_context_limits();
	$context = null === $base ? wpagent_normalized_site_context() : $base;
	foreach ( $limits as $field => $limit ) {
		if ( ! array_key_exists( $field, $input ) ) {
			continue;
		}
		$value = wpagent_state_text( $input[ $field ], $limit, $field );
		if ( is_wp_error( $value ) ) {
			return $value;
		}
		$context[ $field ] = $value;
	}
	return $context;
}

/** @return array|WP_Error */
function wpagent_exec_update_site_context( $input ) {
	$context = wpagent_validate_site_context( $input );
	if ( is_wp_error( $context ) ) {
		return $context;
	}
	$persisted = wpagent_state_persist_options(
		array( WPAGENT_COMPANION_OPT_SITE_CONTEXT => $context ),
		'wpagent_site_context_persist_failed',
		__( 'Site Context konnte nicht verifiziert gespeichert werden.', 'wp-agent-companion' )
	);
	if ( is_wp_error( $persisted ) ) {
		return $persisted;
	}
	return array( 'context' => $context, 'verified' => true );
}
