"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
  breakfastChildren: Omit<BreakfastPackage, "requestedPrice"> | null | undefined
  breakfastIncluded: boolean
  childrenInRoom: Child[] | undefined
  currency: string
  nights: number
}

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

  const breakfastBuffet = intl.formatMessage({
    defaultMessage: "Breakfast buffet",
  })

  const adultsMsg = intl.formatMessage(
    {
      defaultMessage:
        "Breakfast ({totalAdults, plural, one {# adult} other {# adults}}) x {totalBreakfasts}",
    },
    { totalAdults: adults, totalBreakfasts: nights }
  )

  let kidsMsg = ""
  if (childrenInRoom?.length) {
    kidsMsg = intl.formatMessage(
      {
        defaultMessage:
          "Breakfast ({totalChildren, plural, one {# child} other {# children}}) x {totalBreakfasts}",
      },
      {
        totalChildren: childrenInRoom.length,
        totalBreakfasts: nights,
      }
    )
  }

  if (breakfastIncluded) {
    const included = intl.formatMessage({ defaultMessage: "Included" })
    return (
      <Tbody>
        <RegularRow label={adultsMsg} value={included} />
        {childrenInRoom?.length ? (
          <RegularRow label={kidsMsg} value={included} />
        ) : null}
        <BoldRow
          label={breakfastBuffet}
          value={formatPrice(intl, 0, currency)}
        />
      </Tbody>
    )
  }

  if (breakfast) {
    const adultPricePerNight = breakfast.localPrice.price * adults
    const breakfastAdultsPricePerNight = formatPrice(
      intl,
      adultPricePerNight,
      breakfast.localPrice.currency
    )

    const { payingChildren, freeChildren } = childrenInRoom.reduce(
      (total, child) => {
        if (child.age >= 4) {
          total.payingChildren = total.payingChildren + 1
        } else {
          total.freeChildren = total.freeChildren + 1
        }
        return total
      },
      { payingChildren: 0, freeChildren: 0 }
    )

    const childrenPrice = breakfastChildren?.localPrice.price || 0
    const childrenPricePerNight = childrenPrice * payingChildren

    const childCurrency =
      breakfastChildren?.localPrice.currency ?? breakfast.localPrice.currency

    const breakfastChildrenPricePerNight = formatPrice(
      intl,
      childrenPricePerNight,
      childCurrency
    )

    const totalAdultsPrice = adultPricePerNight * nights
    const totalChildrenPrice = childrenPricePerNight * nights
    const breakfastTotalPrice = formatPrice(
      intl,
      totalAdultsPrice + totalChildrenPrice,
      breakfast.localPrice.currency
    )

    const freeChildrenMsg = intl.formatMessage(
      {
        defaultMessage:
          "Breakfast ({totalChildren, plural, one {# child} other {# children}}) x {totalBreakfasts}",
      },
      {
        totalChildren: freeChildren,
        totalBreakfasts: nights,
      }
    )

    return (
      <Tbody border>
        <RegularRow
          label={intl.formatMessage(
            {
              defaultMessage:
                "Breakfast ({totalAdults, plural, one {# adult} other {# adults}}) x {totalBreakfasts}",
            },
            { totalAdults: adults, totalBreakfasts: nights }
          )}
          value={breakfastAdultsPricePerNight}
        />
        {breakfastChildren ? (
          <RegularRow
            label={intl.formatMessage(
              {
                defaultMessage:
                  "Breakfast ({totalChildren, plural, one {# child} other {# children}}) x {totalBreakfasts}",
              },
              {
                totalChildren: payingChildren,
                totalBreakfasts: nights,
              }
            )}
            value={breakfastChildrenPricePerNight}
          />
        ) : null}
        {breakfastChildren && freeChildren ? (
          <RegularRow
            label={`${freeChildrenMsg} (0-3)`}
            value={formatPrice(intl, 0, breakfast.localPrice.currency)}
          />
        ) : null}
        {childrenInRoom?.length && !breakfastChildren ? (
          <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={breakfastBuffet} value={breakfastTotalPrice} />
      </Tbody>
    )
  }

  if (breakfast === false) {
    const noBreakfast = intl.formatMessage({
      defaultMessage: "No breakfast",
    })
    return (
      <Tbody>
        <BoldRow label={breakfastBuffet} value={noBreakfast} />
      </Tbody>
    )
  }

  return null
}
