"use client"

import { useIntl } from "react-intl"

import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { Typography } from "@scandic-hotels/design-system/Typography"

import { CancellationRuleEnum, ChildBedTypeEnum } from "@/constants/booking"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"

import Modal from "@/components/Modal"
import Button from "@/components/TempDesignSystem/Button"
import Link from "@/components/TempDesignSystem/Link"
import { formatPrice } from "@/utils/numberFormatting"

import Breakfast from "./Breakfast"
import RoomSkeletonLoader from "./RoomSkeletonLoader"

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

import type { BookingConfirmationReceiptRoomProps } from "@/types/components/hotelReservation/bookingConfirmation/receipt"

export default function ReceiptRoom({
  roomIndex,
}: BookingConfirmationReceiptRoomProps) {
  const intl = useIntl()
  const { room, currencyCode, isVatCurrency } = useBookingConfirmationStore(
    (state) => ({
      room: state.rooms[roomIndex],
      currencyCode: state.currencyCode,
      isVatCurrency: state.isVatCurrency,
    })
  )

  if (!room) {
    return <RoomSkeletonLoader />
  }

  const childBedCrib = room.childBedPreferences.find(
    (c) => c.bedType === ChildBedTypeEnum.Crib
  )

  const childBedExtraBed = room.childBedPreferences.find(
    (c) => c.bedType === ChildBedTypeEnum.ExtraBed
  )

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

  const guestsParts = [adultsMsg]
  if (room.childrenAges?.length) {
    const childrenMsg = intl.formatMessage(
      {
        defaultMessage:
          "{totalChildren, plural, one {# child} other {# children}}",
      },
      { totalChildren: room.childrenAges.length }
    )
    guestsParts.push(childrenMsg)
  }

  const guests = guestsParts.join(", ")

  return (
    <article className={styles.room}>
      <header className={styles.roomHeader}>
        <Typography variant="Body/Paragraph/mdRegular">
          <p className={styles.uiTextHighContrast}>{room.name}</p>
        </Typography>
        {room.rateDefinition.isMemberRate ? (
          <div className={styles.memberPrice}>
            <Typography variant="Body/Paragraph/mdRegular">
              <p className={styles.red}>{room.formattedRoomCost}</p>
            </Typography>
          </div>
        ) : (
          <Typography variant="Body/Paragraph/mdRegular">
            <p className={styles.uiTextHighContrast}>
              {room.formattedRoomCost}
            </p>
          </Typography>
        )}
        <Typography variant="Body/Paragraph/mdRegular">
          <p className={styles.uiTextMediumContrast}>{guests}</p>
        </Typography>
        <Typography variant="Body/Paragraph/mdRegular">
          <p className={styles.uiTextMediumContrast}>
            {room.rateDefinition.cancellationText}
          </p>
        </Typography>
        <Modal
          trigger={
            <Button intent="text" className={styles.termsLink}>
              <Link
                color="Text/Interactive/Secondary"
                href=""
                size="small"
                textDecoration="underline"
              >
                {intl.formatMessage({
                  defaultMessage: "Reservation policy",
                })}
                <MaterialIcon icon="info" color="CurrentColor" />
              </Link>
            </Button>
          }
          title={
            (isVatCurrency
              ? room.rateDefinition.cancellationText
              : room.rateDefinition.title) || ""
          }
          subtitle={
            room.rateDefinition.cancellationRule ===
            CancellationRuleEnum.CancellableBefore6PM
              ? intl.formatMessage({
                  defaultMessage: "Pay later",
                })
              : intl.formatMessage({
                  defaultMessage: "Pay now",
                })
          }
        >
          <div className={styles.terms}>
            {room.rateDefinition.generalTerms?.map((info) => (
              <Typography
                key={info}
                className={styles.termsText}
                variant="Body/Paragraph/mdRegular"
              >
                <span>
                  <MaterialIcon
                    icon="check"
                    color="Icon/Feedback/Success"
                    size={20}
                    className={styles.termsIcon}
                  />
                  {info}
                </span>
              </Typography>
            ))}
          </div>
        </Modal>
      </header>
      {room.roomFeatures
        ? room.roomFeatures.map((feature) => (
            <div className={styles.entry} key={feature.code}>
              <div>
                <Typography variant="Body/Paragraph/mdRegular">
                  <p className={styles.uiTextHighContrast}>
                    {feature.description}
                  </p>
                </Typography>
              </div>

              <Typography variant="Body/Paragraph/mdRegular">
                <p className={styles.uiTextHighContrast}>
                  {formatPrice(intl, feature.totalPrice, feature.currency)}
                </p>
              </Typography>
            </div>
          ))
        : null}
      <div className={styles.entry}>
        <Typography variant="Body/Paragraph/mdRegular">
          <p className={styles.uiTextHighContrast}>{room.bedDescription}</p>
        </Typography>
        <Typography variant="Body/Paragraph/mdRegular">
          <p className={styles.uiTextHighContrast}>
            {formatPrice(intl, 0, currencyCode)}
          </p>
        </Typography>
      </div>
      {childBedCrib ? (
        <div className={styles.entry}>
          <div>
            <Typography variant="Body/Paragraph/mdRegular">
              <p className={styles.uiTextHighContrast}>
                {intl.formatMessage(
                  {
                    defaultMessage: "Crib (child) × {count}",
                  },
                  { count: childBedCrib.quantity }
                )}
              </p>
            </Typography>
            <Typography variant="Body/Supporting text (caption)/smRegular">
              <p className={styles.uiTextHighContrast}>
                {intl.formatMessage({
                  defaultMessage: "Based on availability",
                })}
              </p>
            </Typography>
          </div>
          <Typography variant="Body/Paragraph/mdRegular">
            <p className={styles.uiTextHighContrast}>
              {formatPrice(intl, 0, currencyCode)}
            </p>
          </Typography>
        </div>
      ) : null}
      {childBedExtraBed ? (
        <div className={styles.entry}>
          <div>
            <Typography variant="Body/Paragraph/mdRegular">
              <p className={styles.uiTextHighContrast}>
                {intl.formatMessage(
                  {
                    defaultMessage: "Extra bed (child) × {count}",
                  },
                  {
                    count: childBedExtraBed.quantity,
                  }
                )}
              </p>
            </Typography>
          </div>
          <Typography variant="Body/Paragraph/mdRegular">
            <p className={styles.uiTextHighContrast}>
              {formatPrice(intl, 0, currencyCode)}
            </p>
          </Typography>
        </div>
      ) : null}
      <Breakfast
        breakfast={room.breakfast}
        breakfastIncluded={room.breakfastIncluded}
        guests={guests}
      />
    </article>
  )
}
