<?php
/** Read abilities for WooCommerce product shipping classes. */

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

function wpagent_ability_list_product_shipping_classes(): array {
	return array(
		'label'               => __( 'Produkt-Versandklassen auflisten', 'wp-agent-companion' ),
		'description'         => __( 'Listet WooCommerce-Produkt-Versandklassen mit sicherer Objektbelegung und Konflikt-Hash auf.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array(
			'type'                 => 'object',
			'properties'           => array(
				'search'   => array( 'type' => 'string', 'maxLength' => 100 ),
				'offset'   => array( 'type' => 'integer', 'minimum' => 0, 'default' => 0 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 100 ),
			),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'                 => 'object',
			'properties'           => array(
				'shipping_classes' => array( 'type' => 'array', 'items' => wpagent_wc_shipping_class_schema() ),
				'count'            => array( 'type' => 'integer', 'minimum' => 0 ),
			),
			'required'             => array( 'shipping_classes', 'count' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_list_product_shipping_classes',
		'permission_callback' => 'wpagent_perm_wc_manage_shipping_classes',
		'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_shipping_classes( $input = array() ) {
	$permitted = wpagent_wc_require_shipping_class_permission();
	if ( is_wp_error( $permitted ) ) {
		return $permitted;
	}
	$available = wpagent_wc_require_shipping_class_runtime();
	if ( is_wp_error( $available ) ) {
		return $available;
	}
	$input    = is_array( $input ) ? $input : array();
	$per_page = min( 100, max( 1, (int) ( $input['per_page'] ?? 100 ) ) );
	$offset   = max( 0, (int) ( $input['offset'] ?? 0 ) );
	$args     = array(
		'taxonomy'   => WPAGENT_WC_SHIPPING_CLASS_TAXONOMY,
		'hide_empty' => false,
		'number'     => $per_page,
		'offset'     => $offset,
		'orderby'    => 'name',
		'order'      => 'ASC',
	);
	if ( isset( $input['search'] ) && '' !== trim( (string) $input['search'] ) ) {
		$args['search'] = trim( (string) $input['search'] );
	}
	$terms = get_terms( $args );
	if ( is_wp_error( $terms ) ) {
		return $terms;
	}
	$counts = wpagent_wc_shipping_class_relation_counts( $terms );
	if ( is_wp_error( $counts ) ) {
		return $counts;
	}
	$shipping_classes = array_map(
		static fn( WP_Term $term ): array => wpagent_wc_shipping_class_payload( $term, (int) ( $counts[ (int) $term->term_taxonomy_id ] ?? 0 ) ),
		$terms
	);
	return array( 'shipping_classes' => $shipping_classes, 'count' => count( $shipping_classes ) );
}

function wpagent_ability_get_product_shipping_class(): array {
	return array(
		'label'               => __( 'Produkt-Versandklasse lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest eine WooCommerce-Produkt-Versandklasse frisch mit Konflikt-Hash.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array( 'type' => 'object', 'properties' => array( 'shipping_class_id' => array( 'type' => 'integer', 'minimum' => 1 ) ), 'required' => array( 'shipping_class_id' ), 'additionalProperties' => false ),
		'output_schema'       => array( 'type' => 'object', 'properties' => array( 'shipping_class' => wpagent_wc_shipping_class_schema() ), 'required' => array( 'shipping_class' ), 'additionalProperties' => false ),
		'execute_callback'    => 'wpagent_exec_get_product_shipping_class',
		'permission_callback' => 'wpagent_perm_wc_manage_shipping_classes',
		'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_shipping_class( $input ) {
	$permitted = wpagent_wc_require_shipping_class_permission();
	if ( is_wp_error( $permitted ) ) {
		return $permitted;
	}
	$available = wpagent_wc_require_shipping_class_runtime();
	if ( is_wp_error( $available ) ) {
		return $available;
	}
	$shipping_class_id = absint( is_array( $input ) ? ( $input['shipping_class_id'] ?? 0 ) : 0 );
	$term              = wpagent_wc_shipping_class_record( $shipping_class_id );
	if ( ! $term ) {
		return new WP_Error( 'wpagent_wc_shipping_class_not_found', __( 'WooCommerce-Produkt-Versandklasse nicht gefunden.', 'wp-agent-companion' ), array( 'status' => 404 ) );
	}
	$payload = wpagent_wc_shipping_class_payload_for_term( $term );
	return is_wp_error( $payload ) ? $payload : array( 'shipping_class' => $payload );
}
