<?php
/**
 * WooCommerce operation-handler extension seam.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChangesWooCommerce\CoreCompat\WooCommerce;

use ScheduledChanges\Domain\Operation\Operation;
use ScheduledChanges\Domain\Operation\TargetDefinition;
use ScheduledChanges\Execution\ExecutionState;
use ScheduledChanges\Execution\OperationExecutionResult;
use ScheduledChanges\Execution\ProviderOperationHandler;
use ScheduledChanges\Field\FieldRegistry;
use ScheduledChanges\Provider\ExecutionContext;
use ScheduledChanges\Provider\ProviderRegistry;
use ScheduledChanges\Repository\RunRepository;
use ScheduledChanges\Rollback\CreatedTargetRollbackHandler;
use ScheduledChangesWooCommerce\CoreCompat\Discovery\TaxonomyTermService;
use ScheduledChangesWooCommerce\CoreCompat\WooCommerce\Downloads\DownloadCodec;
use ScheduledChangesWooCommerce\CoreCompat\WooCommerce\Inventory\InventoryAdjustmentService;
use ScheduledChangesWooCommerce\CoreCompat\WooCommerce\Variations\VariationService;

/** Thin public adapter delegating WooCommerce execution to its focused runner. */
final class WooCommerceOperationHandler implements ProviderOperationHandler, CreatedTargetRollbackHandler {
	private WooCommerceOperationExecutor $executor;

	public function __construct(
		ProviderRegistry $providers,
		FieldRegistry $fields,
		?VariationService $variations = null,
		?InventoryAdjustmentService $inventory = null,
		?ProductSynchronizer $synchronizer = null,
		?DownloadCodec $downloads = null,
		?TaxonomyTermService $taxonomy_terms = null,
		?RunRepository $runs = null,
		bool $inventory_enabled = true,
		bool $variations_enabled = true
	) {
		$this->executor = new WooCommerceOperationExecutor(
			$providers,
			$fields,
			$variations,
			$inventory,
			$synchronizer,
			$downloads,
			$taxonomy_terms,
			$runs,
			$inventory_enabled,
			$variations_enabled
		);
	}

	public function supports( Operation $operation ): bool {
		return $this->executor->supports( $operation );
	}

	public function apply( Operation $operation, TargetDefinition $target, ExecutionContext $context, ExecutionState $state ): OperationExecutionResult {
		return $this->executor->apply( $operation, $target, $context, $state );
	}

	public function supportsCreatedTarget( Operation $operation ): bool {
		return $this->executor->supportsCreatedTarget( $operation );
	}

	/**
	 * Registers a target created by a delegated operation.
	 *
	 * @param array<string,array<string,mixed>> $source_results Source execution results.
	 */
	public function registerCreatedTarget( Operation $operation, array $source_results, ExecutionState $state ): void {
		$this->executor->registerCreatedTarget( $operation, $source_results, $state );
	}

	/**
	 * @param array<string,array<string,mixed>> $source_results Source execution results.
	 * @return array<string,mixed>
	 */
	public function rollbackCreatedTarget( Operation $operation, array $source_results, string $policy ): array {
		return $this->executor->rollbackCreatedTarget( $operation, $source_results, $policy );
	}
}
