<?php
/** Read abilities for WooCommerce global product attributes. */

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

function wpagent_ability_list_product_attributes(): array {
	return array(
		'label'               => __( 'Globale Produktattribute auflisten', 'wp-agent-companion' ),
		'description'         => __( 'Listet WooCommerce-Produktattribute über die öffentliche Attribut-API mit Konflikt-Hashes auf.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => false, 'default' => array() ),
		'output_schema'       => array(
			'type'                 => 'object',
			'properties'           => array(
				'attributes' => array( 'type' => 'array', 'items' => wpagent_wc_attribute_schema() ),
				'count'      => array( 'type' => 'integer' ),
			),
			'required'             => array( 'attributes', 'count' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_list_product_attributes',
		'permission_callback' => 'wpagent_perm_wc_manage_product_terms',
		'meta'                => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_list_product_attributes( $input = array() ) {
	unset( $input );
	$available = wpagent_wc_require_attribute_runtime();
	if ( is_wp_error( $available ) ) {
		return $available;
	}
	$records     = array_values( wc_get_attribute_taxonomies() );
	$taxonomies  = array_map( static fn( $record ): string => wc_attribute_taxonomy_name( (string) ( $record->attribute_name ?? '' ) ), $records );
	$term_counts = wpagent_wc_attribute_term_counts( $taxonomies );
	$attributes  = array_map(
		static function ( $record ) use ( $term_counts ): array {
			$taxonomy = wc_attribute_taxonomy_name( (string) ( $record->attribute_name ?? '' ) );
			return wpagent_wc_attribute_payload( $record, $term_counts[ $taxonomy ] ?? 0 );
		},
		$records
	);
	return array( 'attributes' => $attributes, 'count' => count( $attributes ) );
}

function wpagent_ability_get_product_attribute(): array {
	return array(
		'label'               => __( 'Globales Produktattribut lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest ein globales WooCommerce-Produktattribut frisch über die öffentliche Attribut-API.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array(
			'type'                 => 'object',
			'properties'           => array( 'attribute_id' => array( 'type' => 'integer', 'minimum' => 1 ) ),
			'required'             => array( 'attribute_id' ),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'                 => 'object',
			'properties'           => array( 'attribute' => wpagent_wc_attribute_schema() ),
			'required'             => array( 'attribute' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_get_product_attribute',
		'permission_callback' => 'wpagent_perm_wc_manage_product_terms',
		'meta'                => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_product_attribute( $input ) {
	$available = wpagent_wc_require_attribute_runtime();
	if ( is_wp_error( $available ) ) {
		return $available;
	}
	$record = wpagent_wc_attribute_record( absint( is_array( $input ) ? ( $input['attribute_id'] ?? 0 ) : 0 ) );
	if ( ! $record ) {
		return new WP_Error( 'wpagent_wc_attribute_not_found', __( 'Globales WooCommerce-Produktattribut nicht gefunden.', 'wp-agent-companion' ), array( 'status' => 404 ) );
	}
	return array( 'attribute' => wpagent_wc_attribute_payload( $record ) );
}
