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

import { Typography } from "@scandic-hotels/design-system/Typography"
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>
          <Typography variant="Title/xs">
            <h2 style={{ color: "var(--Text-Heading)" }}>
              {intl.formatMessage(
                {
                  id: "booking.roomIndex",
                  defaultMessage: "Room {roomIndex}",
                },
                {
                  roomIndex: 1,
                }
              )}
            </h2>
          </Typography>
        </Header>
      ) : null}

      <SelectedRoom />

      {room.bedTypes ? (
        <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 user={user} />
      </Section>
    </section>
  )
}
