import { useEffect, useState } from "react"
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 [isVisible, setIsVisible] = useState(false)

  useEffect(() => {
    const timer = setTimeout(() => setIsVisible(true), 0)
    return () => clearTimeout(timer)
  }, [])

  const {
    member,
    public: publicRate,
    features,
    roomType,
    priceName,
  } = rateSummary
  const priceToShow = isUserLoggedIn && member ? 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")

  const showMemberDiscountBanner = member && !isUserLoggedIn

  const summaryPriceTex = `${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 }
        )}`
      : ""
  }`

  return (
    <div className={styles.summary} data-visible={isVisible}>
      {showMemberDiscountBanner && (
        <div className={styles.memberDiscountBannerMobile}>
          <Footnote color="burgundy">
            {intl.formatMessage({
              id: "Join or log in while booking for member pricing.",
            })}
          </Footnote>
        </div>
      )}
      <div className={styles.summaryText}>
        <Subtitle color="uiTextHighContrast">{roomType}</Subtitle>
        <Body color="uiTextMediumContrast">{priceName}</Body>
      </div>
      <div className={styles.summaryPriceContainer}>
        {showMemberDiscountBanner && (
          <div className={styles.memberDiscountBannerDesktop}>
            <Footnote color="burgundy">
              {intl.formatMessage<React.ReactNode>(
                {
                  id: "To get the member price <span>{amount} {currency}</span>, log in or join when completing the booking.",
                },
                {
                  span: (str) => (
                    <Caption color="red" type="bold" asChild>
                      <span>{str}</span>
                    </Caption>
                  ),
                  amount: member.localPrice.pricePerStay,
                  currency: member.localPrice.currency,
                }
              )}
            </Footnote>
          </div>
        )}
        <div className={styles.summaryPriceTextDesktop}>
          <Body>
            {intl.formatMessage<React.ReactNode>(
              { id: "<b>Total price</b> (incl VAT)" },
              { b: (str) => <b>{str}</b> }
            )}
          </Body>
          <Caption color="uiTextMediumContrast">{summaryPriceTex}</Caption>
        </div>
        <div className={styles.summaryPrice}>
          <div className={styles.summaryPriceTextDesktop}>
            <Subtitle
              color={isUserLoggedIn ? "red" : "uiTextHighContrast"}
              textAlign="right"
            >
              {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}
            >
              {summaryPriceTex}
            </Footnote>
          </div>
          {isPetRoomSelected && (
            <div className={styles.petInfo}>
              <Body
                color="uiTextHighContrast"
                textTransform="bold"
                textAlign="right"
              >
                + {petRoomPrice} {petRoomCurrency}
              </Body>
              <Body color="uiTextMediumContrast" textAlign="right">
                {intl.formatMessage({ id: "Pet charge" })}
              </Body>
            </div>
          )}
          <Button type="submit" theme="base" className={styles.continueButton}>
            {intl.formatMessage({ id: "Continue" })}
          </Button>
        </div>
      </div>
    </div>
  )
}
