<?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' ),
			'type'     => array( 'type' => 'string' ),
			'label'    => array( 'type' => 'string' ),
			'variants' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'additionalProperties' => true ) ),
		),
		'additionalProperties' => true,
	);
}

/** @return array<string,mixed> */
function wpagent_elementor_global_classes_output_schema(): array {
	return array(
		'type'       => 'object',
		'properties' => array(
			'kit_id'            => array( 'type' => 'integer' ),
			'elementor_version' => array( 'type' => 'string' ),
			'breakpoints'       => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
			'classes'           => array( 'type' => 'array', 'items' => wpagent_elementor_global_class_schema() ),
			'total'             => array( 'type' => 'integer' ),
			'hash'              => array( 'type' => 'string' ),
		),
	);
}

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 );
}
