<?php
/**
 * Reusable product inventory module.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChangesWooCommerce\CoreCompat\WooCommerce\Inventory;

/** Owns stock fields, idempotent adjustments, and restock templates. */
final class InventoryModule {
	public const MODULE_ID = 'scheduled-changes-inventory';

	/** @return string[] */
	public function fieldKeys(): array {
		return array(
			'inventory.manage_stock',
			'inventory.stock_quantity',
			'inventory.stock_status',
			'inventory.backorders',
			'inventory.sold_individually',
			'inventory.low_stock_threshold',
		);
	}

	/** Creates the idempotent absolute/delta adjustment service. */
	public function createAdjustmentService(): InventoryAdjustmentService {
		return new InventoryAdjustmentService();
	}

	/** @return array<int,array<string,mixed>> */
	public function templates(): array {
		$field = static fn ( string $field_path, string $operation_type ): array => array(
			'provider_id'    => 'woocommerce-core',
			'target_type'    => 'variation',
			'field_path'     => $field_path,
			'operation_type' => $operation_type,
		);

		return array(
			array(
				'schema_version' => 1,
				'id'             => 'restock-variation',
				'name'           => 'Restock variation',
				'description'    => 'Restore WooCommerce variation stock quantity and status.',
				'operations'     => array(
					$field( 'product.inventory.stock_quantity', 'set_stock' ),
					$field( 'product.inventory.stock_status', 'set_field' ),
				),
			),
		);
	}
}
