<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase, WordPress.Files.FileName.InvalidClassFileName
/**
 * Legacy backward-compatible wrapper for wholesale pricing.
 *
 * @package Autoship\Modules\WholesalePricing\Compatibility
 * @since   2.13.0
 */

namespace Autoship\Modules\WholesalePricing\Compatibility;

use Autoship\Core\Plugin;
use Autoship\Modules\WholesalePricing\Services\WholesaleNoticeManager;
use Autoship\Modules\WholesalePricing\Services\WholesalePricingServiceInterface;

/**
 * Wraps the modern WholesalePricingService for legacy callers.
 *
 * Keeps the same method signatures as Autoship_Cloud_Wholesale_Pricing
 * to allow gradual migration of external callers.
 *
 * @package Autoship\Modules\WholesalePricing\Compatibility
 * @since   2.13.0
 */
class LegacyWholesalePricingAdapter {

	/**
	 * Validate wholesale price using the modern service.
	 *
	 * Preserves the legacy polymorphic return type: boolean true on pass,
	 * or a notice array on failure.
	 *
	 * @param bool     $apply_price          Whether to apply wholesale pricing.
	 * @param float    $items_total_price    Total price for this item.
	 * @param array    $user_wholesale_role  The user's wholesale role.
	 * @param int      $item_total           Total quantity for this item.
	 * @param int|bool $product_id           The product ID.
	 *
	 * @return array|bool Notice array on failure, boolean true on success.
	 */
	public function autoship_validate_wholesale_price( $apply_price, $items_total_price, $user_wholesale_role, $item_total = 1, $product_id = false ) {
		$container = Plugin::get_service_container();
		$service   = $container->get( WholesalePricingServiceInterface::class );
		$result    = $service->validate( $user_wholesale_role, $item_total, $items_total_price, $product_id );

		if ( $result->passed() ) {
			return $apply_price;
		}

		$notice_manager = $container->get( WholesaleNoticeManager::class );
		$notice         = $notice_manager->build_notice( $result, $product_id, $user_wholesale_role );

		$notice = apply_filters(
			'autoship_filter_wholesale_price_requirement_failure_notice',
			$notice,
			$result->get_requirements()->get_minimum_order_items(),
			$result->get_requirements()->get_minimum_order_price(),
			$item_total,
			$items_total_price,
			$user_wholesale_role
		);

		return ! empty( $notice ) ? $notice : $apply_price;
	}
}
