<?php
/**
 * WooCommerce Product Quantity in Cart condition
 *
 * @link       https://personalizewp.com/
 * @since      3.2.0 Merged into PWP
 * @since      3.0.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Rule_Conditions/WooCommerce
 */

namespace PersonalizeWP\Rule_Conditions\WooCommerce;

use PersonalizeWP\Rule_Conditions\RuleCondition;
use PersonalizeWP\Rule_Conditions\Dependency\Woocommerce;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Checks if a specific product quantity in the cart meets the specified condition
 */
class Product_Quantity_In_Cart extends RuleCondition {

	/**
	 * Container of WC dependency and helper functions
	 *
	 * @var Woocommerce
	 */
	private object $woocommerce;

	/**
	 * Condition identifier
	 *
	 * @var string
	 */
	public string $identifier = 'woocommerce_product_quantity_in_cart';

	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct();

		$this->category    = __( 'WooCommerce', 'personalizewp' );
		$this->description = __( 'Quantity of Product items in Cart', 'personalizewp' );

		$this->measure_key = 'product_id';

		$this->comparators = [
			'more_than' => _x( 'More Than', 'Comparator', 'personalizewp' ),
			'less_than' => _x( 'Less Than', 'Comparator', 'personalizewp' ),
			'equals'    => _x( 'Is Equal To', 'Comparator', 'personalizewp' ),
		];

		$this->comparison_values = [];
		$this->comparison_type   = 'text';
	}

	/**
	 * Test product quantity in cart against condition
	 *
	 * @param string $comparator Comparator test to run
	 * @param string $value      Quantity value to compare against
	 * @param string $action     Action to take, unused
	 * @param mixed  $meta       Additional meta data containing product_id(s)
	 *
	 * @return bool
	 */
	public function matchesCriteria( $comparator, $value, $action, $meta = [] ): bool {
		// Get product IDs from meta
		$product_ids = [];

		$meta = (array) $meta;
		if ( isset( $meta[ $this->measure_key ] ) ) {
			$product_ids = explode( ',', $meta[ $this->measure_key ] );
			// Sanitise the product IDs.
			$product_ids = array_filter( array_unique( array_map( 'intval', $product_ids ) ) );
		}

		if ( empty( $product_ids ) ) {
			return false;
		}

		// Get cart contents via WooCommerce dependency
		$cart_products = $this->woocommerce->get_product_information_from_cart();

		// Calculate total quantity of all specified products
		$total_quantity = 0;
		foreach ( $product_ids as $product_id ) {
			if ( isset( $cart_products[ $product_id ]['quantity'] ) ) {
				$total_quantity += (int) $cart_products[ $product_id ]['quantity'];
			}
		}

		// Convert comparison value to integer
		$comparison_value = (int) $value;

		// Perform comparison based on the comparator
		switch ( $comparator ) {
			case 'more_than':
				return $total_quantity > $comparison_value;

			case 'less_than':
				return $total_quantity < $comparison_value;

			case 'equals':
				return $total_quantity === $comparison_value;
		}

		return false;
	}

	/**
	 * Array of dependencies for the condition to be used
	 *
	 * @return array
	 */
	public function dependencies(): array {
		$this->woocommerce = Woocommerce::instance();

		return [
			$this->woocommerce,
		];
	}
}
