<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase, WordPress.Files.FileName.InvalidClassFileName
/**
 * Resolves wholesale minimum order requirements.
 *
 * @package Autoship\Modules\WholesalePricing\Services
 * @since   2.13.0
 */

namespace Autoship\Modules\WholesalePricing\Services;

use Autoship\Domain\WholesalePricing\WholesaleRequirements;

/**
 * Resolves the effective wholesale minimum order requirements
 * from site-wide, per-role, per-user, and per-product settings.
 *
 * Extracted from the legacy resolve_wholesale_requirements() method.
 *
 * @package Autoship\Modules\WholesalePricing\Services
 * @since   2.13.0
 */
class WholesaleRequirementsResolver {

	/**
	 * Resolve wholesale requirements for a given user/role/product.
	 *
	 * @param array    $user_wholesale_role The user's wholesale role.
	 * @param int|bool $product_id          The product ID (for per-product MOQ).
	 *
	 * @return WholesaleRequirements
	 */
	public function resolve( array $user_wholesale_role, $product_id = false ): WholesaleRequirements {
		$wholesale_settings  = $this->get_site_settings();
		$minimum_order_items = null;
		$minimum_order_price = null;
		$conditional_logic   = $wholesale_settings['minimum_requirements_conditional_logic'];
		$active_currency     = get_woocommerce_currency();

		if ( $product_id && $this->uses_premium() ) {
			if ( $wholesale_settings['minimum_cart_items'] && is_numeric( $wholesale_settings['minimum_cart_items'] ) ) {
				$moq = (int) $wholesale_settings['minimum_cart_items'];
			} else {
				$moq = get_post_meta( $product_id, $user_wholesale_role[0] . '_wholesale_minimum_order_quantity', true );
			}
			$minimum_order_items = ( is_numeric( $moq ) ) ? (int) $moq : null;

			if ( $wholesale_settings['minimum_cart_price'] && is_numeric( $wholesale_settings['minimum_cart_price'] ) ) {
				$minimum_order_price = (int) $wholesale_settings['minimum_cart_price'];
			}
		}

		if ( 'yes' === $wholesale_settings['override_per_wholesale_role'] ) {
			$per_role_requirements = $wholesale_settings['per_wholesale_role_order_requirement'];
			if ( ! is_array( $per_role_requirements ) ) {
				$per_role_requirements = array();
			}

			if ( array_key_exists( $user_wholesale_role[0], $per_role_requirements ) ) {
				$minimum_order_items = $per_role_requirements[ $user_wholesale_role[0] ]['minimum_order_quantity'];
				$minimum_order_price = $per_role_requirements[ $user_wholesale_role[0] ]['minimum_order_subtotal'];
				$conditional_logic   = $per_role_requirements[ $user_wholesale_role[0] ]['minimum_order_logic'];
			}
		}

		$user_min_order_qty_applied   = false;
		$user_min_order_price_applied = false;

		if ( 'yes' === $wholesale_settings['user_min_order_qty_override'] ) {
			if ( is_numeric( $wholesale_settings['user_min_order_qty'] ) || empty( $wholesale_settings['user_min_order_qty'] ) ) {
				$minimum_order_items        = $wholesale_settings['user_min_order_qty'];
				$user_min_order_qty_applied = true;
			}
		}

		if ( 'yes' === $wholesale_settings['user_min_order_price_override'] ) {
			if ( is_numeric( $wholesale_settings['user_min_order_price'] ) || empty( $wholesale_settings['user_min_order_price'] ) ) {
				$minimum_order_price          = $wholesale_settings['user_min_order_price'];
				$user_min_order_price_applied = true;
			}
		}

		if ( $user_min_order_qty_applied && $user_min_order_price_applied ) {
			if ( in_array( $wholesale_settings['user_min_order_logic'], array( 'and', 'or' ), true ) ) {
				$conditional_logic = $wholesale_settings['user_min_order_logic'];
			}
		}

		// Aelia Currency Switcher compatibility.
		if ( class_exists( 'WWP_ACS_Integration_Helper' ) && \WWP_ACS_Integration_Helper::aelia_currency_switcher_active() ) {
			if ( $active_currency !== $wholesale_settings['shop_base_currency'] ) {
				$minimum_order_price = \WWP_ACS_Integration_Helper::convert( $minimum_order_price, $active_currency, $wholesale_settings['shop_base_currency'] );
			}
		}

		$min_items = is_numeric( $minimum_order_items ) ? (int) $minimum_order_items : null;
		$min_price = is_numeric( $minimum_order_price ) ? (float) $minimum_order_price : null;

		return new WholesaleRequirements( $min_items, $min_price, $conditional_logic ? $conditional_logic : 'and' );
	}

	/**
	 * Get the site's wholesale settings.
	 *
	 * @param int|null $user_id The user ID. Defaults to current user.
	 *
	 * @return array The site settings.
	 */
	public function get_site_settings( ?int $user_id = null ): array {
		if ( null === $user_id ) {
			$user_id = get_current_user_id();
		}

		return array(
			'minimum_cart_items'                     => trim( get_option( 'wwpp_settings_minimum_order_quantity' ) ),
			'minimum_cart_price'                     => trim( get_option( 'wwpp_settings_minimum_order_price' ) ),
			'minimum_requirements_conditional_logic' => get_option( 'wwpp_settings_minimum_requirements_logic' ),
			'override_per_wholesale_role'            => get_option( 'wwpp_settings_override_order_requirement_per_role', false ),
			'per_wholesale_role_order_requirement'   => get_option( 'wwpp_option_wholesale_role_order_requirement_mapping', array() ),
			'shop_base_currency'                     => get_option( 'woocommerce_currency' ),
			'user_min_order_qty_override'            => get_user_meta( $user_id, 'wwpp_override_min_order_qty', true ),
			'user_min_order_qty'                     => get_user_meta( $user_id, 'wwpp_min_order_qty', true ),
			'user_min_order_price_override'          => get_user_meta( $user_id, 'wwpp_override_min_order_price', true ),
			'user_min_order_price'                   => get_user_meta( $user_id, 'wwpp_min_order_price', true ),
			'user_min_order_logic'                   => get_user_meta( $user_id, 'wwpp_min_order_logic', true ),
		);
	}

	/**
	 * Check if WooCommerce Wholesale Prices Premium is active.
	 *
	 * @return bool
	 */
	private function uses_premium(): bool {
		return class_exists( 'WooCommerceWholeSalePricesPremium' );
	}
}
