<?php
/** Read Elementor v4 Global Classes through the active native kit. */

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

/** @return array<string,mixed> */
function wpagent_elementor_global_class_schema(): array {
	return array(
		'type'                 => 'object',
		'properties'           => array(
			'id'       => array( 'type' => 'string', 'pattern' => '^[a-z][a-z0-9_-]{0,63}$' ),
			'type'     => array( 'type' => 'string', 'enum' => array( 'class' ) ),
			'label'    => array( 'type' => 'string', 'minLength' => 2, 'maxLength' => 50 ),
			'sync_to_v3' => array( 'type' => 'boolean' ),
			'variants' => array(
				'type'     => 'array',
				'maxItems' => WPAGENT_ELEMENTOR_GLOBAL_CLASSES_MAX_VARIANTS,
				'items'    => array(
					'type'                 => 'object',
					'properties'           => array(
						'meta'  => array(
							'type'                 => 'object',
							'properties'           => array(
								'breakpoint' => array( 'type' => 'string', 'maxLength' => 64 ),
								'state'      => array( 'type' => array( 'string', 'null' ), 'maxLength' => 64 ),
							),
							'required'             => array( 'breakpoint', 'state' ),
							'additionalProperties' => false,
						),
						'props' => wpagent_elementor_json_document_schema(),
					),
					'required'             => array( 'meta', 'props' ),
					'additionalProperties' => false,
				),
			),
			'has_unsupported_custom_css' => array( 'type' => 'boolean' ),
		),
		'required'             => array( 'id', 'type', 'label', 'sync_to_v3', 'variants', 'has_unsupported_custom_css' ),
		'additionalProperties' => false,
	);
}

/** @return array<string,mixed> */
function wpagent_elementor_global_classes_output_schema(): array {
	return array(
		'type'                 => 'object',
		'properties'           => array(
			'kit_id'            => array( 'type' => 'integer', 'minimum' => 1 ),
			'elementor_version' => array( 'type' => 'string', 'maxLength' => 64 ),
			'breakpoints'       => array( 'type' => 'array', 'minItems' => 1, 'maxItems' => WPAGENT_ELEMENTOR_GLOBAL_CLASSES_MAX_VARIANTS, 'items' => array( 'type' => 'string', 'maxLength' => 64 ) ),
			'classes'           => array( 'type' => 'array', 'maxItems' => WPAGENT_ELEMENTOR_GLOBAL_CLASSES_MAX_ITEMS, 'items' => wpagent_elementor_global_class_schema() ),
			'total'             => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => WPAGENT_ELEMENTOR_GLOBAL_CLASSES_MAX_ITEMS ),
			'hash'              => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
		'required'             => array( 'kit_id', 'elementor_version', 'breakpoints', 'classes', 'total', 'hash' ),
		'additionalProperties' => false,
	);
}

function wpagent_ability_get_elementor_global_classes(): array {
	return array(
		'label'               => __( 'Elementor v4 Global Classes lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest Elementors native v4 Global Classes, aktive Breakpoints und einen Konflikt-Hash aus dem aktiven Kit.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => false ),
		'output_schema'       => wpagent_elementor_global_classes_output_schema(),
		'execute_callback'    => 'wpagent_exec_get_elementor_global_classes',
		'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_global_classes( $input ) {
	if ( ! current_user_can( 'manage_options' ) ) {
		return new WP_Error( 'wpagent_forbidden', __( 'Keine Berechtigung.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	$snapshot = wpagent_elementor_global_classes_snapshot();
	return is_wp_error( $snapshot ) ? $snapshot : wpagent_elementor_global_classes_state( $snapshot );
}
