<?php
/** Read Elementor's global colors and typography through the active native kit. */

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

/** @return array<string,mixed> */
function wpagent_elementor_design_system_output_schema(): array {
	$color = array(
		'type'                 => 'object',
		'properties'           => array(
			'id'    => array( 'type' => 'string', 'pattern' => '^[a-z0-9][a-z0-9_-]{0,63}$' ),
			'title' => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 100 ),
			'value' => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 100 ),
		),
		'required'             => array( 'id', 'title', 'value' ),
		'additionalProperties' => false,
	);
	$typography = array(
		'type'                 => 'object',
		'properties'           => array(
			'id'     => array( 'type' => 'string', 'pattern' => '^[a-z0-9][a-z0-9_-]{0,63}$' ),
			'title'  => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 100 ),
			'styles' => wpagent_elementor_design_style_schema(),
		),
		'required'             => array( 'id', 'title', 'styles' ),
		'additionalProperties' => false,
	);
	return array(
		'type'                 => 'object',
		'properties'           => array(
			'kit_id' => array( 'type' => 'integer', 'minimum' => 1 ),
			'elementor_version'  => array( 'type' => 'string', 'maxLength' => 64 ),
			'system_colors'      => array( 'type' => 'array', 'minItems' => 4, 'maxItems' => 4, 'items' => $color ),
			'custom_colors'      => array( 'type' => 'array', 'maxItems' => WPAGENT_ELEMENTOR_DESIGN_MAX_TOKENS, 'items' => $color ),
			'system_typography'  => array( 'type' => 'array', 'minItems' => 4, 'maxItems' => 4, 'items' => $typography ),
			'custom_typography'  => array( 'type' => 'array', 'maxItems' => WPAGENT_ELEMENTOR_DESIGN_MAX_TOKENS, 'items' => $typography ),
			'fallback_font'      => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 200 ),
			'hash'               => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
		'required'             => array( 'kit_id', 'elementor_version', 'system_colors', 'custom_colors', 'system_typography', 'custom_typography', 'fallback_font', 'hash' ),
		'additionalProperties' => false,
	);
}

function wpagent_ability_get_elementor_design_system(): array {
	return array(
		'label' => __( 'Elementor-Designsystem lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest globale System- und eigene Farben, Typografie, responsive Typografiewerte und Fallback-Schrift aus Elementors aktivem Kit mit Konflikt-Hash.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => false ),
		'output_schema' => wpagent_elementor_design_system_output_schema(),
		'execute_callback' => 'wpagent_exec_get_elementor_design_system',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_elementor_design_system( $input ) {
	$kit = wpagent_elementor_design_active_kit();
	return is_wp_error( $kit ) ? $kit : wpagent_elementor_design_system_state( $kit );
}
