"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/RoomOne"
import Header from "../Room/Header"
import Section from "../Section"
import SelectedRoom from "../SelectedRoom"
import { getBedTypeInfoText } from "./utils"

import type { User } from "@scandic-hotels/trpc/types/user"

export default function RoomOne({ user }: { user: User | null }) {
  const intl = useIntl()
  const { room } = useRoomContext()
  const { breakfastPackages, isMultiroom } = useEnterDetailsStore((state) => ({
    breakfastPackages: state.breakfastPackages,
    isMultiroom: state.rooms.length > 1,
  }))

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

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

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

  return (
    <section id="room-1">
      {isMultiroom ? (
        <Header>
          <Title level="h2" as="h4">
            {intl.formatMessage(
              {
                defaultMessage: "Room {roomIndex}",
              },
              {
                roomIndex: 1,
              }
            )}
          </Title>
        </Header>
      ) : null}

      <SelectedRoom />

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

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

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