<?php
/**
 * Price helper for recommendations engine.
 *
 * @package PRECO
 */

namespace PRECO\Includes\Frontend\Engine\Filters;

use PRECO\Includes\Frontend\Engine\Filters\Price\BoundsFilter;
use PRECO\Includes\Frontend\Engine\Filters\Price\BoundsResolver;
use PRECO\Includes\Frontend\Engine\Filters\Price\EffectivePriceResolver;

defined( 'ABSPATH' ) || exit;

/**
 * Price helper for recommendations engine.
 */
class PriceHelper {
	/**
	 * Effective price resolver.
	 *
	 * @var EffectivePriceResolver
	 */
	private $prices;

	/**
	 * Price bounds resolver.
	 *
	 * @var BoundsResolver
	 */
	private $bounds_resolver;

	/**
	 * Price bounds filter.
	 *
	 * @var BoundsFilter
	 */
	private $bounds_filter;

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->prices          = new EffectivePriceResolver();
		$this->bounds_resolver = new BoundsResolver( $this->prices );
		$this->bounds_filter   = new BoundsFilter( $this->prices );
	}


	/**
	 * Get an effective numeric product price suitable for filtering/sorting.
	 *
	 * - Simple products: current effective price (sale if on sale).
	 * - Variable products: MIN effective variation price.
	 * - Fallback to common meta keys if needed.
	 *
	 * @param int $product_id Product ID.
	 * @return float|null
	 */
	public function get_effective_price_for_product_id( $product_id ) {
		return $this->prices->resolve_for_product_id( $product_id );
	}

	/**
	 * Resolve min/max price bounds from rules.
	 *
	 * @param array $rules Rules array.
	 * @param array $context Context.
	 * @return array{min:float|null,max:float|null,max_exclusive:bool}|array
	 */
	public function resolve_bounds_from_rules( $rules, $context ) {
		return $this->bounds_resolver->resolve_from_rules( $rules, $context );
	}

	/**
	 * Filter product IDs by price bounds using effective WooCommerce price.
	 *
	 * @param int[] $ids Product IDs.
	 * @param array $bounds Bounds.
	 * @return int[]
	 */
	public function filter_ids_by_bounds( $ids, $bounds ) {
		return $this->bounds_filter->filter_ids_by_bounds( $ids, $bounds );
	}
}
