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

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 CorporateChequePriceType {
  corporateCheque?: {
    additionalPricePerStay?: number
    currency?: CurrencyEnum
    numberOfCheques: number
  }
}

interface CorporateChequePriceProps extends SharedPriceRowProps {
  currency: string
  price: CorporateChequePriceType["corporateCheque"]
}

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

  if (!price) {
    return null
  }

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

  const additionalPricePerStay = price.additionalPricePerStay

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

  const additionalCurrency = price.currency ?? currency
  let averagePricePerNight = `${averageChequesPerNight} ${CurrencyEnum.CC}`
  if (averageAdditionalPricePerNight) {
    averagePricePerNight = `${averagePricePerNight} + ${averageAdditionalPricePerNight} ${additionalCurrency}`
  }

  return (
    <>
      <BoldRow
        label={intl.formatMessage({ defaultMessage: "Room charge" })}
        value={formatPrice(
          intl,
          price.numberOfCheques,
          CurrencyEnum.CC,
          additionalPricePerStay,
          additionalCurrency
        )}
      />
      {nights > 1 ? (
        <RegularRow label={averagePriceTitle} value={averagePricePerNight} />
      ) : null}
      <BedTypeRow bedType={bedType} currency={currency} />
      <PackagesRow packages={packages} />
    </>
  )
}
