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

import { useEnterDetailsStore } from "@/stores/enter-details"

import BedType from "@/components/HotelReservation/EnterDetails/BedType"
import Breakfast from "@/components/HotelReservation/EnterDetails/Breakfast"
import Details from "@/components/HotelReservation/EnterDetails/Details/RoomOne"
import Header from "@/components/HotelReservation/EnterDetails/Room/Header"
import Section from "@/components/HotelReservation/EnterDetails/Section"
import SelectedRoom from "@/components/HotelReservation/EnterDetails/SelectedRoom"
import Title from "@/components/TempDesignSystem/Text/Title"
import { useRoomContext } from "@/contexts/Details/Room"

import { getBedTypeInfoText } from "./utils"

import { ChildBedMapEnum } from "@/types/components/bookingWidget/enums"
import { StepEnum } from "@/types/enums/step"
import type { SafeUser } from "@/types/user"

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

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

  const bedTypeInfoText = getBedTypeInfoText(
    intl,
    !!hasChildWithExtraBed,
    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={StepEnum.selectBed}
        >
          <BedType />
        </Section>
      ) : null}

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

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