"use client"

import { useIntl } from "react-intl"

import { useBookingConfirmationStore } from "@/stores/booking-confirmation"

import SkeletonShimmer from "@/components/SkeletonShimmer"
import Body from "@/components/TempDesignSystem/Text/Body"
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"
import { formatPrice } from "@/utils/numberFormatting"

import styles from "./paymentDetails.module.css"

export default function PaymentDetails() {
  const intl = useIntl()

  const { rooms, currencyCode } = useBookingConfirmationStore((state) => ({
    rooms: state.rooms,
    currencyCode: state.currencyCode,
  }))

  const hasAllRoomsLoaded = rooms.every((room) => room)
  const grandTotal = rooms.reduce((acc, room) => {
    const reservationTotalPrice = room?.totalPrice || 0
    return acc + reservationTotalPrice
  }, 0)

  return (
    <div className={styles.details}>
      <Subtitle color="uiTextHighContrast" type="two">
        {intl.formatMessage({ id: "Payment details" })}
      </Subtitle>
      <div className={styles.payment}>
        {hasAllRoomsLoaded ? (
          <Body color="uiTextHighContrast">
            {intl.formatMessage(
              { id: "{amount} has been paid" },
              {
                amount: formatPrice(intl, grandTotal, currencyCode),
              }
            )}
          </Body>
        ) : (
          <SkeletonShimmer width={"100%"} />
        )}
      </div>
    </div>
  )
}
