import { useIntl } from "react-intl"

import { dt } from "@/lib/dt"

import Button from "@/components/TempDesignSystem/Button"
import Body from "@/components/TempDesignSystem/Text/Body"
import Caption from "@/components/TempDesignSystem/Text/Caption"
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"

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

import type { RateSummaryProps } from "@/types/components/hotelReservation/selectRate/rateSummary"
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"

export default function RateSummary({
  rateSummary,
  isUserLoggedIn,
  packages,
  roomsAvailability,
}: RateSummaryProps) {
  const intl = useIntl()
  const {
    member,
    public: publicRate,
    features,
    roomType,
    priceName,
  } = rateSummary
  const priceToShow = isUserLoggedIn ? member : publicRate

  const isPetRoomSelected = features.some(
    (feature) => feature.code === RoomPackageCodeEnum.PET_ROOM
  )

  const petRoomPackage = packages.find(
    (pkg) => pkg.code === RoomPackageCodeEnum.PET_ROOM
  )

  const petRoomPrice = petRoomPackage?.localPrice.totalPrice ?? null
  const petRoomCurrency = petRoomPackage?.localPrice.currency ?? null

  const checkInDate = new Date(roomsAvailability.checkInDate)
  const checkOutDate = new Date(roomsAvailability.checkOutDate)
  const nights = dt(checkOutDate).diff(dt(checkInDate), "days")

  return (
    <div className={styles.summary}>
      <div className={styles.summaryText}>
        <Subtitle color="uiTextHighContrast">{roomType}</Subtitle>
        <Body color="uiTextMediumContrast">{priceName}</Body>
      </div>
      <div className={styles.summaryPrice}>
        <div className={styles.summaryPriceTextDesktop}>
          <Subtitle color={isUserLoggedIn ? "red" : "uiTextHighContrast"}>
            {priceToShow?.localPrice.pricePerStay}{" "}
            {priceToShow?.localPrice.currency}
          </Subtitle>
          <Body color="uiTextMediumContrast">
            {intl.formatMessage({ id: "Approx." })}{" "}
            {priceToShow?.requestedPrice?.pricePerStay}{" "}
            {priceToShow?.requestedPrice?.currency}
          </Body>
        </div>
        <div className={styles.summaryPriceTextMobile}>
          <Caption color="uiTextHighContrast">
            {intl.formatMessage({ id: "Total price" })}
          </Caption>
          <Subtitle color={isUserLoggedIn ? "red" : "uiTextHighContrast"}>
            {priceToShow?.localPrice.pricePerStay}{" "}
            {priceToShow?.localPrice.currency}
          </Subtitle>
          <Footnote
            color="uiTextMediumContrast"
            className={styles.summaryPriceTextMobile}
          >
            {intl.formatMessage(
              { id: "booking.nights" },
              { totalNights: nights }
            )}
            ,{" "}
            {intl.formatMessage(
              { id: "booking.adults" },
              { totalAdults: roomsAvailability.occupancy?.adults }
            )}
            {roomsAvailability.occupancy?.children?.length && (
              <>
                ,{" "}
                {intl.formatMessage(
                  { id: "booking.children" },
                  { totalChildren: roomsAvailability.occupancy.children.length }
                )}
              </>
            )}
          </Footnote>
        </div>
        {isPetRoomSelected && (
          <div className={styles.petInfo}>
            <Body color="uiTextHighContrast" textTransform="bold">
              + {petRoomPrice} {petRoomCurrency}
            </Body>
            <Body color="uiTextMediumContrast">
              {intl.formatMessage({ id: "Pet charge" })}
            </Body>
          </div>
        )}
        <Button type="submit" theme="base" className={styles.continueButton}>
          {intl.formatMessage({ id: "Continue" })}
        </Button>
      </div>
    </div>
  )
}
