/**
 * Compute the balance due for a booking — total minus discounts minus
 * completed non-discount payments plus completed refunds, floored at 0.
 */
export function computeBalanceDue(booking: {
	total_amount: number;
	discounts?: Array<{ discount_amount: number }> | null;
	payments?: Array<{ payment_method: string; status: string; amount: number }> | null;
	refunds?: Array<{ status: string; amount: number }> | null;
}): number {
	const totalRefunds = (booking.refunds ?? [])
		.filter((r) => r.status === 'completed')
		.reduce((sum, r) => sum + r.amount, 0);
	return Math.max(
		0,
		booking.total_amount -
			(booking.discounts ?? []).reduce((sum, d) => sum + d.discount_amount, 0) -
			(booking.payments ?? [])
				.filter((p) => p.payment_method !== 'discount' && p.status === 'completed')
				.reduce((sum, p) => sum + p.amount, 0) +
			totalRefunds
	);
}
