"use client"

import { trpc } from "@/lib/trpc/client"
import useSidePeekStore from "@/stores/sidepeek"

import BookedRoomSidePeek from "@/components/SidePeeks/BookedRoomSidePeek"
import HotelSidePeek from "@/components/SidePeeks/HotelSidePeek"
import RoomSidePeek from "@/components/SidePeeks/RoomSidePeek"
import useLang from "@/hooks/useLang"

export default function HotelReservationSidePeek() {
  const activeSidePeek = useSidePeekStore((state) => state.activeSidePeek)
  const hotelId = useSidePeekStore((state) => state.hotelId)
  const confirmationNumber = useSidePeekStore(
    (state) => state.confirmationNumber
  )
  const roomTypeCode = useSidePeekStore((state) => state.roomTypeCode)
  const showCTA = useSidePeekStore((state) => state.showCTA)
  const user = useSidePeekStore((state) => state.user)
  const close = useSidePeekStore((state) => state.closeSidePeek)
  const lang = useLang()

  const { data: hotelData } = trpc.hotel.get.useQuery(
    {
      hotelId: hotelId ?? "",
      language: lang,
      isCardOnlyPayment: false,
    },
    {
      enabled: !!hotelId,
    }
  )

  const selectedRoom = hotelData?.roomCategories.find((room) =>
    room.roomTypes.some((type) => type.code === roomTypeCode)
  )

  if (activeSidePeek) {
    return (
      <>
        {hotelData && (
          <HotelSidePeek
            additionalHotelData={hotelData.additionalData}
            hotel={hotelData.hotel}
            activeSidePeek={activeSidePeek}
            close={close}
            showCTA={showCTA}
          />
        )}
        {selectedRoom && (
          <RoomSidePeek
            room={selectedRoom}
            activeSidePeek={activeSidePeek}
            close={close}
          />
        )}
        {selectedRoom && (
          <BookedRoomSidePeek
            room={selectedRoom}
            activeSidePeek={activeSidePeek}
            close={close}
            user={user}
            confirmationNumber={confirmationNumber}
          />
        )}
      </>
    )
  }

  return null
}
