"use client"
import { useIntl } from "react-intl"

import { sumPackages } from "@/components/HotelReservation/utils"
import { formatPrice } from "@/utils/numberFormatting"

import BoldRow from "../Bold"
import RegularRow from "../Regular"
import BedTypeRow from "./BedType"
import PackagesRow from "./Packages"

import { CurrencyEnum } from "@/types/enums/currency"
import type { SharedPriceRowProps } from "./price"

export interface VoucherPriceType {
  voucher?: {
    numberOfVouchers: number
  }
}

interface VoucherPriceProps extends SharedPriceRowProps {
  currency: string
  nights: number
  price: VoucherPriceType["voucher"]
}

export default function VoucherPrice({
  bedType,
  currency,
  nights,
  packages,
  price,
}: VoucherPriceProps) {
  const intl = useIntl()

  if (!price) {
    return null
  }

  const averagePriceTitle = intl.formatMessage({
    defaultMessage: "Average price per night",
  })
  const pkgsSum = sumPackages(packages)

  let additionalPricePerStay
  if (pkgsSum.price) {
    additionalPricePerStay = pkgsSum.price
  }

  const averageAdditionalPricePerNight = additionalPricePerStay
    ? Math.ceil(additionalPricePerStay / nights)
    : null

  let averagePricePerNight = `${price.numberOfVouchers} ${CurrencyEnum.Voucher}`
  if (averageAdditionalPricePerNight) {
    averagePricePerNight = `${averagePricePerNight} + ${averageAdditionalPricePerNight} ${pkgsSum.currency ?? currency}`
  }

  return (
    <>
      <RegularRow label={averagePriceTitle} value={averagePricePerNight} />
      <BedTypeRow bedType={bedType} currency={currency} />
      <PackagesRow packages={packages} />
      <BoldRow
        label={intl.formatMessage({ defaultMessage: "Room charge" })}
        value={formatPrice(
          intl,
          price.numberOfVouchers,
          CurrencyEnum.Voucher,
          additionalPricePerStay,
          pkgsSum.currency ?? currency
        )}
      />
    </>
  )
}
