<?php
/** Read abilities for native WordPress Global Style Variations. */

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

function wpagent_gs_variation_summary_schema(): array {
	return array(
		'type'       => 'object',
		'properties' => array(
			'variation_id'      => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			'title'             => array( 'type' => 'string' ),
			'description'       => array( 'type' => 'string' ),
			'scope'             => array( 'type' => 'string', 'enum' => array( 'full', 'color', 'typography' ) ),
			'bytes'             => array( 'type' => 'integer' ),
			'settings_sections' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
			'style_sections'    => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
			'has_custom_css'    => array( 'type' => 'boolean' ),
		),
		'required'             => array( 'variation_id', 'title', 'description', 'scope', 'bytes', 'settings_sections', 'style_sections', 'has_custom_css' ),
		'additionalProperties' => false,
	);
}

function wpagent_ability_list_global_style_variations(): array {
	return array(
		'label'               => __( 'Native Stilvarianten auflisten', 'wp-agent-companion' ),
		'description'         => __( 'Listet die native Standardauswahl und die vom aktiven Theme gelieferten vollständigen, Farb- und Typografievarianten mit stabilen, Theme-gebundenen IDs.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array(
			'type'       => 'object',
			'properties' => array(
				'scope' => array( 'type' => 'string', 'enum' => array( 'all', 'full', 'color', 'typography' ), 'default' => 'all' ),
			),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'       => 'object',
			'properties' => array(
				'theme'      => array( 'type' => 'string' ),
				'block_theme' => array( 'type' => 'boolean' ),
				'state_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
				'count'      => array( 'type' => 'integer' ),
				'items'      => array( 'type' => 'array', 'items' => wpagent_gs_variation_summary_schema() ),
			),
			'required'             => array( 'theme', 'block_theme', 'state_hash', 'count', 'items' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_list_global_style_variations',
		'permission_callback' => 'wpagent_perm_edit_theme_options',
		'meta'                => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_list_global_style_variations( $input ) {
	$input = is_array( $input ) ? $input : array();
	$scope = isset( $input['scope'] ) && is_string( $input['scope'] ) ? $input['scope'] : 'all';
	if ( ! in_array( $scope, array( 'all', 'full', 'color', 'typography' ), true ) ) {
		return new WP_Error( 'wpagent_gs_variation_scope', __( 'scope muss all, full, color oder typography sein.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	$state = wpagent_gs_state();
	if ( is_wp_error( $state ) ) {
		return $state;
	}
	$catalog = wpagent_gs_variation_catalog();
	if ( is_wp_error( $catalog ) ) {
		return $catalog;
	}
	if ( (string) $state['theme'] !== (string) get_stylesheet() ) {
		return new WP_Error( 'wpagent_gs_variation_theme_changed', __( 'Das aktive Theme wurde während des Lesens gewechselt. Lies die Stilvarianten erneut.', 'wp-agent-companion' ), array( 'status' => 409 ) );
	}
	$items = array();
	foreach ( $catalog as $record ) {
		if ( 'all' !== $scope && $scope !== $record['scope'] ) {
			continue;
		}
		unset( $record['variation'] );
		$items[] = $record;
	}
	return array(
		'theme'       => (string) $state['theme'],
		'block_theme' => function_exists( 'wp_is_block_theme' ) && wp_is_block_theme(),
		'state_hash'  => (string) $state['state_hash'],
		'count'       => count( $items ),
		'items'       => $items,
	);
}

function wpagent_ability_get_global_style_variation(): array {
	return array(
		'label'               => __( 'Native Stilvariante lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest eine einzelne Theme-Stilvariante vollständig und typgetreu, bevor sie angewandt wird.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array(
			'type'       => 'object',
			'properties' => array( 'variation_id' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ) ),
			'required'   => array( 'variation_id' ),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'       => 'object',
			'properties' => array(
				'theme'        => array( 'type' => 'string' ),
				'variation_id' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
				'title'        => array( 'type' => 'string' ),
				'description'  => array( 'type' => 'string' ),
				'scope'        => array( 'type' => 'string', 'enum' => array( 'full', 'color', 'typography' ) ),
				'bytes'        => array( 'type' => 'integer' ),
				'variation'    => array( 'type' => 'object' ),
			),
			'required'             => array( 'theme', 'variation_id', 'title', 'description', 'scope', 'bytes', 'variation' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_get_global_style_variation',
		'permission_callback' => 'wpagent_perm_edit_theme_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_global_style_variation( $input ) {
	$input  = is_array( $input ) ? $input : array();
	$record = wpagent_gs_find_variation( $input['variation_id'] ?? '' );
	if ( is_wp_error( $record ) ) {
		return $record;
	}
	return array(
		'theme'        => (string) get_stylesheet(),
		'variation_id' => $record['variation_id'],
		'title'        => $record['title'],
		'description'  => $record['description'],
		'scope'        => $record['scope'],
		'bytes'        => $record['bytes'],
		'variation'    => $record['variation'],
	);
}
