<?php
// If this file is called directly, abort.
if (! defined('ABSPATH')) {
	exit;
}

/**
 * Main render entry point for the Product Price widget.
 *
 * @param \GGM_Commerce_Base_Widget $widget The widget instance.
 */
function ggmc_product_price_render( $widget ) {
	// Get product data (includes preview product support)
	$product_data = $widget->get_product_data();

	$price_html = '<span class="price-placeholder">$99.99</span>'; // Placeholder
	if ($product_data && isset($product_data->price)) {
		$price_html = ggmc_product_price_format_price( $widget, $product_data );
	} elseif (!ggmc_is_editor_mode()) {
		$price_html = '';
	}

	$widget->add_render_attribute('price_wrapper', 'class', 'product-price');
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_render_attribute_string() and $price_html are pre-escaped
	echo '<div ' . $widget->get_render_attribute_string('price_wrapper') . '>' . $price_html . '</div>';
}

/**
 * Format price with sale price support, tax suffix, and variation price range.
 * Uses per-product tax rates for accurate price display.
 *
 * @param \GGM_Commerce_Base_Widget $widget  The widget instance.
 * @param object                    $product Product data object.
 * @return string Formatted price HTML.
 */
function ggmc_product_price_format_price( $widget, $product ) {
	$tax = ggm_commerce_tax();
	$product_id = isset($product->id) ? (int) $product->id : 0;
	$settings = $widget->get_settings_for_display();

	// Check if this product has variations
	$price_range = ggmc_product_price_get_variation_price_range( $product_id );

	// Get tax suffix if enabled
	$suffix = '';
	if ($tax->should_show_suffix()) {
		$suffix = ' <span class="ggmc-tax-suffix">' . esc_html($tax->get_suffix_text()) . '</span>';
	}

	// If product has variations, use variable price format settings
	if ($price_range && $price_range['min'] !== null) {
		$format = isset($settings['variable_price_format']) ? $settings['variable_price_format'] : 'from';
		$min_display = $tax->get_product_display_price($price_range['min'], $product_id);
		$max_display = $tax->get_product_display_price($price_range['max'], $product_id);

		if ('from' === $format) {
			// "From $X" format - always show lowest price with label
			$from_label = isset($settings['from_label_text']) ? $settings['from_label_text'] : 'From';
			return '<span class="price-from-label">' . esc_html($from_label) . '</span> ' .
			       '<span class="current-price">' . esc_html($tax->format_price($min_display['price'])) . '</span>' . $suffix;
		} else {
			// Range format "$X - $Y"
			$separator = isset($settings['range_separator']) ? $settings['range_separator'] : ' - ';
			// If min equals max, just show single price without separator
			if (!$price_range['has_range']) {
				return '<span class="current-price">' . esc_html($tax->format_price($min_display['price'])) . '</span>' . $suffix;
			}
			return '<span class="current-price">' . esc_html($tax->format_price($min_display['price'])) . '</span>' .
			       '<span class="price-range-separator">' . esc_html($separator) . '</span>' .
			       '<span class="current-price">' . esc_html($tax->format_price($max_display['price'])) . '</span>' . $suffix;
		}
	}

	// Standard price display (no variations)
	$regular_display = $tax->get_product_display_price($product->price, $product_id);

	if (empty($product->sale_price) || $product->sale_price >= $product->price) {
		// No sale price or sale price is higher/equal than regular price
		return '<span class="current-price">' . esc_html($tax->format_price($regular_display['price'])) . '</span>' . $suffix;
	} else {
		// Has sale price - calculate sale display price using same per-product tax rate
		$sale_display = $tax->get_product_display_price($product->sale_price, $product_id);
		return '<span class="sale-price">' . esc_html($tax->format_price($sale_display['price'])) . '</span>' . $suffix . ' ' .
			   '<span class="original-price">' . esc_html($tax->format_price($regular_display['price'])) . '</span>';
	}
}

/**
 * Get price range for products with variations.
 *
 * @param int $product_id Product ID.
 * @return array|null Array with min, max, has_range or null if no variations.
 */
function ggmc_product_price_get_variation_price_range( $product_id ) {
	// Check if variations feature is active
	if (!class_exists('GGM_Commerce_License') || !GGM_Commerce_License::instance()->is_widget_activated('product-variations')) {
		return null;
	}

	$variations_manager = GGM_Commerce_Variations::instance();

	// Check if product has variations
	if (!$variations_manager->product_has_variations($product_id)) {
		return null;
	}

	// Get price range from variations manager
	return $variations_manager->get_display_price_range($product_id);
}
