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

namespace Autoship\Modules\WholesalePricing\Services;

/**
 * Handles display/presentation hook callbacks for wholesale pricing.
 *
 * Covers product page price arrays, discount strings, and formatted price HTML.
 *
 * @package Autoship\Modules\WholesalePricing\Services
 * @since   2.13.0
 */
class WholesaleDisplayHandler {

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

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

	/**
	 * Adds wholesale rate and discount percentage to the price array.
	 *
	 * Hooked to autoship_all_prices_array.
	 *
	 * @param array       $prices     The current list of prices for the product.
	 * @param int         $product_id The current product ID.
	 * @param \WC_Product $product    The WooCommerce product.
	 *
	 * @return array The adjusted prices array.
	 */
	public function all_prices_array_w_wholesale( $prices, $product_id, $product ) {
		if ( ! is_product() && ! is_cart() ) {
			return $prices;
		}

		$user_wholesale_role  = $this->pricing->get_user_role();
		$base_wholesale_price = $this->pricing->retrieve_base_price( $product_id );
		$wholesale_price      = ! empty( $user_wholesale_role ) ? $this->pricing->adjust_price( $product_id, $base_wholesale_price ) : null;

		if ( ! is_null( $wholesale_price ) ) {

			$prices['autoship_checkout_price'] = $this->pricing->adjust_price( $product_id, $base_wholesale_price, $prices['autoship_checkout_price'] );

			$prices['autoship_checkout_display_price'] = wc_get_price_to_display( $product, array( 'price' => $prices['autoship_checkout_price'] ) );

			$prices['checkout_price'] = autoship_checkout_price(
				$product,
				array(
					'price'    => $prices['price'],
					'discount' => $prices['autoship_checkout_price'],
				)
			);

			$prices['checkout_price_is_autoship'] = $prices['checkout_price'] !== $prices['price'];

			$prices['checkout_display_price'] = wc_get_price_to_display( $product, array( 'price' => $prices['checkout_price'] ) );

			$prices['autoship_recurring_price'] = $this->pricing->adjust_price( $product_id, $base_wholesale_price, $prices['autoship_recurring_price'] );

			$prices['autoship_recurring_display_price'] = wc_get_price_to_display( $product, array( 'price' => $prices['autoship_recurring_price'] ) );

			$prices['wholesale']     = $base_wholesale_price;
			$prices['regular_price'] = $prices['wholesale'];

			$prices['regular_display_price'] = wc_get_price_to_display( $product, array( 'price' => $prices['regular_price'] ) );

			$prices['autoship_percent_discount'] = autoship_percent_discount(
				$product,
				array(
					'price'    => $prices['regular_price'],
					'discount' => $prices['autoship_checkout_price'],
				)
			);

			$prices['autoship_percent_recurring_discount'] = autoship_percent_recurring_discount(
				$product,
				array(
					'price'    => $prices['regular_price'],
					'discount' => $prices['autoship_recurring_price'],
				)
			);

			$prices['wholesale_pct']        = 100 * $this->pricing->get_scheduled_discount_percentage( $product_id, $user_wholesale_role );
			$prices['wholesale_discount']   = $prices['wholesale'] - ( $prices['wholesale'] * ( $prices['wholesale_pct'] / 100 ) );
			$prices['wholesale_product_id'] = $product_id;
		}

		return $prices;
	}

	/**
	 * Adjusts the autoship notice if wholesale prices exist.
	 *
	 * Hooked to autoship_discount_checkout_and_recurring_same.
	 *
	 * @param string      $output  The current autoship notice.
	 * @param array       $strings The array of notice components.
	 * @param array       $prices  The current list of prices for the product.
	 * @param \WC_Product $product The current product.
	 *
	 * @return string The adjusted autoship notice string.
	 */
	public function checkout_recurring_discount_wholesale_string( $output, $strings, $prices, $product ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
		$new_output = $strings['autoship_string'] . sprintf( ' <span class="autoship-checkout-percent-discount">%s%%</span> on Wholesale Orders', $prices['autoship_percent_discount'] ) . $strings['price_string'];

		return ! isset( $prices['wholesale'] ) || is_null( $prices['wholesale'] ) ? $output : $new_output;
	}

	/**
	 * Adjusts the autoship displayed price HTML.
	 *
	 * Hooked to autoship_product_discount_data.
	 *
	 * @param array       $data    The data to use to display.
	 * @param \WC_Product $product The WooCommerce product.
	 *
	 * @return array The adjusted data.
	 */
	public function adjust_displayed_wholesale_string_price( $data, $product ) {
		if ( ! isset( $data['prices']['wholesale'] ) ) {
			return $data;
		}

		$data['discount_display_price']          = $data['discounted_price_html'];
		$data['discount_display_price_selector'] = 'simple' === $product->get_type() ? '.wholesale_price_container .woocommerce-Price-amount.amount > bdi' : '.wholesale_price_container > ins';

		return $data;
	}
}
