function roundMoney(value: number) {
  return Math.round(value * 100) / 100;
}

export function calcQuoteTnd(input: {
  basePriceTnd: number;
  nights: number;
  cleaningFeeTnd: number;
  platformFeeRateTnd: number;
  taxesRateTnd: number;
  discountTnd?: number;
}) {
  const discountTnd = input.discountTnd ?? 0;
  const subtotalTnd = roundMoney(input.basePriceTnd * input.nights);
  const base = roundMoney(subtotalTnd + input.cleaningFeeTnd);
  const platformFeeTnd = roundMoney(base * input.platformFeeRateTnd);
  const taxesTnd = roundMoney(base * input.taxesRateTnd);
  const totalTnd = roundMoney(base + platformFeeTnd + taxesTnd - discountTnd);
  const ownerPayoutTnd = roundMoney(subtotalTnd + input.cleaningFeeTnd - platformFeeTnd);

  return {
    subtotalTnd,
    cleaningFeeTnd: input.cleaningFeeTnd,
    platformFeeTnd,
    taxesTnd,
    discountTnd,
    totalTnd,
    ownerPayoutTnd,
  };
}

export function convertTndToLyd(totalTnd: number, rate: number) {
  return roundMoney(totalTnd * rate);
}
