import type { CustomerInfoModel } from "@pkg/logic/domains/customerinfo/data";
import type { OrderPriceDetailsModel } from "@pkg/logic/domains/order/data/entities";
import type { ProductModel } from "@pkg/logic/domains/product/data";

/**
 * Calculates final prices for a product checkout using OrderPriceDetailsModel
 * @param product - The product being purchased
 * @param customerInfo - Customer information (included for future extensibility)
 * @returns OrderPriceDetailsModel with all price fields calculated
 */
export function calculateFinalPrices(
	product: ProductModel | null,
	customerInfo?: CustomerInfoModel | null,
): OrderPriceDetailsModel {
	if (!product) {
		return {
			currency: "USD",
			basePrice: 0,
			discountAmount: 0,
			displayPrice: 0,
			orderPrice: 0,
			fullfilledPrice: 0,
		};
	}

	const basePrice = product.price || 0;
	const discountPrice = product.discountPrice || 0;

	// Calculate discount amount: if discountPrice is set and less than base price
	const hasDiscount = discountPrice > 0 && discountPrice < basePrice;
	const discountAmount = hasDiscount ? basePrice - discountPrice : 0;

	// Display price is either the discounted price or the base price
	const displayPrice = hasDiscount ? discountPrice : basePrice;

	// For single product checkout:
	// - orderPrice = displayPrice (what customer pays)
	// - fullfilledPrice = displayPrice (same as order price for immediate fulfillment)
	const orderPrice = displayPrice;
	const fullfilledPrice = displayPrice;

	return {
		currency: "USD",
		basePrice,
		discountAmount,
		displayPrice,
		orderPrice,
		fullfilledPrice,
	};
}
