<?php

namespace StoreEngine\Frontend;

use StoreEngine\classes\Exceptions\StoreEngineException;
use StoreEngine\Utils\Formatting;
use StoreEngine\Utils\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Coupon {

	public static function init() {
		add_action( 'storeengine/validate_coupon', [ __CLASS__, 'check_cart_minimum_requirement' ] );
		add_action( 'storeengine/validate_coupon', [ __CLASS__, 'check_prices_requirement' ] );
	}

	/**
	 * @throws StoreEngineException
	 */
	public static function check_cart_minimum_requirement( \StoreEngine\classes\Coupon $coupon ) {
		if ( 'none' !== $coupon->settings['coupon_type_of_min_requirement'] ) {
			$cart          = Helper::cart();
			$cart_subtotal = (float) $cart->get_subtotal();

			self::check_minimum_requirements( $coupon, $cart->get_count(), $cart_subtotal );
		}
	}


	/**
	 * @param \StoreEngine\Classes\Coupon $coupon
	 * @param $total_quantity
	 * @param $subtotal
	 *
	 * @return void
	 * @throws StoreEngineException
	 */
	public static function check_minimum_requirements( \StoreEngine\Classes\Coupon $coupon, $total_quantity, $subtotal ) {
		$minimum_purchase_quantity = $coupon->settings['coupon_min_purchase_quantity'];
		if ( 'quantity' === $coupon->settings['coupon_type_of_min_requirement'] && $total_quantity < $minimum_purchase_quantity ) {
			throw new StoreEngineException( esc_html(
				sprintf(
					// translators: %d: minimum purchase quantity.
					__( 'Sorry, Coupon has minimum purchase quantity of %d', 'storeengine' ),
					$minimum_purchase_quantity
				)
			), 'min-purchase-qty', null, 400 );
		}

		$minimum_purchase_amount = $coupon->settings['coupon_min_purchase_amount'] ?? 0;
		if ( 'amount' === $coupon->settings['coupon_type_of_min_requirement'] && $subtotal < (float) $minimum_purchase_amount ) {
			throw new StoreEngineException( esc_html(
				sprintf(
				// translators: %s: minimum purchase amount.
					__( 'Sorry, Coupon has minimum purchase amount of %s', 'storeengine' ),
					Formatting::price( $minimum_purchase_amount, [
						'in_span' => false
					] )
				)
			), 'min-purchase-amount', null, 400 );
		}
	}

	/**
	 * @throws StoreEngineException
	 */
	public static function check_prices_requirement( \StoreEngine\classes\Coupon $coupon ) {
		if ( empty( $coupon->get_valid_price_data() ) ) {
			return;
		}

		$cart     = Helper::cart();
		$is_valid = false;
		foreach ( $coupon->get_valid_price_data() as $valid_price_data ) {
			if ( $cart->is_product_in_cart( $valid_price_data['product_id'] ) ) {
				if ( empty( $valid_price_data['price_ids'] ) ) {
					$is_valid = true;
					break;
				}

				foreach ( $valid_price_data['price_ids'] as $price_id ) {
					if ( $cart->is_price_in_cart( $price_id ) ) {
						$is_valid = true;
						break;
					}
				}

				if ( $is_valid ) {
					break;
				}
			}
		}

		if ( ! $is_valid ) {
			throw new StoreEngineException( esc_html__( "Sorry, Coupon isn't valid for your cart.", 'storeengine' ), 'min-purchase-amount', null, 400 );
		}
	}

}
