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

import { formatPrice } from "@/utils/numberFormatting"

import BoldRow from "./Row/Bold"
import RegularRow from "./Row/Regular"
import Tbody from "./Tbody"

import type { BreakfastPackage } from "@/types/components/hotelReservation/breakfast"
import type { Child } from "@/types/components/hotelReservation/selectRate/selectRate"

interface BreakfastProps {
  adults: number
  breakfast: Omit<BreakfastPackage, "requestedPrice"> | false | undefined | null
  breakfastIncluded: boolean
  childrenInRoom: Child[] | undefined
  currency: string
  nights: number
}

export default function Breakfast({
  adults,
  breakfast,
  breakfastIncluded,
  childrenInRoom,
  currency,
  nights,
}: BreakfastProps) {
  const intl = useIntl()

  if (breakfastIncluded) {
    const included = intl.formatMessage({ defaultMessage: "Included" })
    return (
      <Tbody border>
        <RegularRow
          label={intl.formatMessage(
            {
              defaultMessage:
                "Breakfast ({totalAdults, plural, one {# adult} other {# adults}}) x {totalBreakfasts}",
            },
            { totalAdults: adults, totalBreakfasts: nights }
          )}
          value={included}
        />
        {childrenInRoom?.length ? (
          <RegularRow
            label={intl.formatMessage(
              {
                defaultMessage:
                  "Breakfast ({totalChildren, plural, one {# child} other {# children}}) x {totalBreakfasts}",
              },
              {
                totalChildren: childrenInRoom.length,
                totalBreakfasts: nights,
              }
            )}
            value={included}
          />
        ) : null}
        <BoldRow
          label={intl.formatMessage({ defaultMessage: "Breakfast charge" })}
          value={formatPrice(intl, 0, currency)}
        />
      </Tbody>
    )
  }

  if (!breakfast) {
    return null
  }

  const breakfastAdultsPricePerNight = formatPrice(
    intl,
    breakfast.localPrice.price * adults,
    breakfast.localPrice.currency
  )
  const breakfastAdultsTotalPrice = formatPrice(
    intl,
    breakfast.localPrice.price * adults * nights,
    breakfast.localPrice.currency
  )

  return (
    <Tbody border>
      <RegularRow
        label={intl.formatMessage(
          {
            defaultMessage:
              "Breakfast ({totalAdults, plural, one {# adult} other {# adults}}) x {totalBreakfasts}",
          },
          { totalAdults: adults, totalBreakfasts: nights }
        )}
        value={breakfastAdultsPricePerNight}
      />
      {childrenInRoom?.length ? (
        <RegularRow
          label={intl.formatMessage(
            {
              defaultMessage:
                "Breakfast ({totalChildren, plural, one {# child} other {# children}}) x {totalBreakfasts}",
            },
            {
              totalChildren: childrenInRoom.length,
              totalBreakfasts: nights,
            }
          )}
          value={formatPrice(intl, 0, breakfast.localPrice.currency)}
        />
      ) : null}
      <BoldRow
        label={intl.formatMessage({ defaultMessage: "Breakfast charge" })}
        value={breakfastAdultsTotalPrice}
      />
    </Tbody>
  )
}
