"use client"
import { useIntl } from "react-intl"

import Title from "@scandic-hotels/design-system/Title"
import { ChildBedMapEnum } from "@scandic-hotels/trpc/enums/childBedMapEnum"

import { useRoomContext } from "../../../contexts/EnterDetails/RoomContext"
import { useEnterDetailsStore } from "../../../stores/enter-details"
import { EnterDetailsStepEnum } from "../../../stores/enter-details/enterDetailsStep"
import BedType from "../BedType"
import Breakfast from "../Breakfast"
import Details from "../Details/Multiroom"
import Header from "../Room/Header"
import Section from "../Section"
import SelectedRoom from "../SelectedRoom"
import { getBedTypeInfoText } from "./utils"

export default function Multiroom() {
  const intl = useIntl()
  const { room, roomNr } = useRoomContext()
  const breakfastPackages = useEnterDetailsStore(
    (state) => state.breakfastPackages
  )

  const showBreakfastStep =
    !room.breakfastIncluded && !!breakfastPackages.length

  const hasChildWithExtraBed = room.childrenInRoom?.some(
    (child) => Number(child.bed) === ChildBedMapEnum.IN_EXTRA_BED
  )

  const bedTypeInfoText = getBedTypeInfoText(
    intl,
    !!hasChildWithExtraBed,
    room.bedTypes.length > 1
  )

  return (
    <section id={`room-${roomNr}`}>
      <Header>
        <Title level="h2" as="h4">
          {intl.formatMessage(
            {
              id: "booking.roomIndex",
              defaultMessage: "Room {roomIndex}",
            },
            {
              roomIndex: roomNr,
            }
          )}
        </Title>
      </Header>

      <SelectedRoom />

      {room.bedTypes.length ? (
        <Section
          header={intl.formatMessage({
            id: "booking.bedPreference",
            defaultMessage: "Bed preference",
          })}
          label={intl.formatMessage({
            id: "enterDetails.bedPreference.description",
            defaultMessage: "Preferred bed type",
          })}
          additionalInfo={bedTypeInfoText}
          step={EnterDetailsStepEnum.selectBed}
        >
          <BedType />
        </Section>
      ) : null}

      {showBreakfastStep ? (
        <Section
          header={intl.formatMessage({
            id: "common.breakfast",
            defaultMessage: "Breakfast",
          })}
          label={intl.formatMessage({
            id: "enterDetails.breakfast.description",
            defaultMessage: "Select breakfast options",
          })}
          step={EnterDetailsStepEnum.breakfast}
        >
          <Breakfast />
        </Section>
      ) : null}

      <Section
        header={intl.formatMessage({
          id: "enterDetails.details.title",
          defaultMessage: "Details",
        })}
        step={EnterDetailsStepEnum.details}
        label={intl.formatMessage({
          id: "enterDetails.details.description",
          defaultMessage: "Contact details",
        })}
      >
        <Details />
      </Section>
    </section>
  )
}
