<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase, WordPress.Files.FileName.InvalidClassFileName
/**
 * Wholesale pricing checkout hook handler.
 *
 * @package Autoship\Modules\WholesalePricing\Services
 * @since   2.13.0
 */

namespace Autoship\Modules\WholesalePricing\Services;

use Autoship\Modules\WholesalePricing\Compatibility\LegacyWholesalePricingAdapter;

/**
 * Handles checkout-specific hook callbacks for wholesale pricing.
 *
 * Covers scheduled order full item data adjustment at checkout.
 *
 * @package Autoship\Modules\WholesalePricing\Services
 * @since   2.13.0
 */
class WholesaleCheckoutHandler {

	/**
	 * The pricing service.
	 *
	 * @var WholesalePricingServiceInterface
	 */
	private WholesalePricingServiceInterface $pricing;

	/**
	 * The legacy adapter for validate calls.
	 *
	 * @var LegacyWholesalePricingAdapter
	 */
	private LegacyWholesalePricingAdapter $legacy_adapter;

	/**
	 * Constructor.
	 *
	 * @param WholesalePricingServiceInterface $pricing        The pricing service.
	 * @param LegacyWholesalePricingAdapter    $legacy_adapter The legacy adapter.
	 */
	public function __construct(
		WholesalePricingServiceInterface $pricing,
		LegacyWholesalePricingAdapter $legacy_adapter
	) {
		$this->pricing        = $pricing;
		$this->legacy_adapter = $legacy_adapter;
	}

	/**
	 * Adjusts the recurring price for scheduled order items at checkout.
	 *
	 * Hooked to autoship_get_scheduled_order_data_full_item_data.
	 *
	 * @param array              $scheduled_order_item_data The current scheduled order line item data.
	 * @param int                $order_id                  The WC Order ID.
	 * @param \WC_Order_Item     $item                      The current WC_Order_Item data.
	 *
	 * @return array The updated scheduled order line item data.
	 */
	public function update_scheduled_order_full_item_data( $scheduled_order_item_data, $order_id, $item ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
		$user_wholesale_role = $this->pricing->get_user_role();

		if ( empty( $user_wholesale_role ) ) {
			return $scheduled_order_item_data;
		}

		$product_id     = $scheduled_order_item_data['product']['id'];
		$product        = wc_get_product( $product_id );
		$qty            = (int) $scheduled_order_item_data['quantity'];
		$adjusted_price = $this->pricing->adjust_price( (int) $product_id );

		if ( ! $this->is_non_zero_empty( $adjusted_price ) && $adjusted_price !== $product->get_price() ) {
			$apply = $this->legacy_adapter->autoship_validate_wholesale_price( true, round( $adjusted_price * $qty, 2 ), $user_wholesale_role, $qty, $product_id );

			if ( true === $apply ) {
				$scheduled_order_item_data['salePrice'] = $adjusted_price;
			}
		}

		return $scheduled_order_item_data;
	}

	/**
	 * Checks for empty value (checks empty, not 0 strings and NULL).
	 *
	 * @param mixed $val The value to check.
	 *
	 * @return bool True if empty or not set, else false.
	 */
	private function is_non_zero_empty( $val ): bool {
		return ! isset( $val ) || ( empty( $val ) && 0 !== $val );
	}
}
