<?php
/**
 * WooCommerce bundle pricing hooks for BoostBot.
 *
 * Applies per-line-item bundle prices when the preset uses the 'line_item' discount method.
 * When the discount method is 'coupon' (default), these hooks are no-ops and existing
 * coupon-based behaviour is preserved.
 *
 * @package BoostBot
 * @since   2.1.2
 */

// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class BoostBot_Bundle_Pricing_Hooks {

	/**
	 * Register WooCommerce hooks.
	 *
	 * @return void
	 */
	public static function init() {
		add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'apply_bundle_prices' ), 20, 1 );
		add_filter( 'woocommerce_cart_item_price', array( __CLASS__, 'display_bundle_price' ), 20, 3 );
	}

	/**
	 * Override the price object on cart items that carry bundle metadata
	 * and whose preset uses the line_item discount method.
	 *
	 * @param WC_Cart $cart WooCommerce cart instance.
	 * @return void
	 */
	public static function apply_bundle_prices( $cart ) {
		if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
			return;
		}
		if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
			return;
		}

		foreach ( $cart->get_cart() as $cart_item ) {
			if ( empty( $cart_item['boostbot_bundle_price'] ) ) {
				continue;
			}

			if ( ! self::should_use_line_item_pricing( $cart_item ) ) {
				continue;
			}

			$bundle_price = floatval( $cart_item['boostbot_bundle_price'] );
			if ( $bundle_price > 0 && $bundle_price < floatval( $cart_item['data']->get_price() ) ) {
				$cart_item['data']->set_price( $bundle_price );
			}
		}
	}

	/**
	 * Show strikethrough original price + discounted bundle price in the cart table.
	 *
	 * @param string $price_html Current price HTML.
	 * @param array  $cart_item  Cart item data.
	 * @param string $cart_item_key Cart item key.
	 * @return string Modified price HTML.
	 */
	public static function display_bundle_price( $price_html, $cart_item, $cart_item_key ) {
		if ( empty( $cart_item['boostbot_bundle_price'] ) || empty( $cart_item['boostbot_original_price'] ) ) {
			return $price_html;
		}

		if ( ! self::should_use_line_item_pricing( $cart_item ) ) {
			return $price_html;
		}

		$original = floatval( $cart_item['boostbot_original_price'] );
		$bundle   = floatval( $cart_item['boostbot_bundle_price'] );

		if ( $bundle >= $original || $bundle <= 0 ) {
			return $price_html;
		}

		return '<del>' . wc_price( $original ) . '</del> <ins>' . wc_price( $bundle ) . '</ins>';
	}

	/**
	 * Determine whether the cart item's bundle should use line-item pricing.
	 * The discount method is stored directly in the cart item metadata
	 * at add-to-cart time, so no extra DB query is needed here.
	 *
	 * @param array $cart_item Cart item data.
	 * @return bool
	 */
	private static function should_use_line_item_pricing( $cart_item ) {
		return ( $cart_item['boostbot_bundle_discount_method'] ?? 'coupon' ) === 'line_item';
	}
}
