import { notFound } from "next/navigation"
import { Suspense } from "react"

import SelectHotel from "@/components/HotelReservation/SelectHotel"
import { SelectHotelSkeleton } from "@/components/HotelReservation/SelectHotel/SelectHotelSkeleton"
import { getHotelReservationQueryParams } from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
import { setLang } from "@/i18n/serverContext"

import { getHotelSearchDetails } from "../utils"

import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
import type { LangParams, PageArgs } from "@/types/params"

export default async function SelectHotelPage({
  params,
  searchParams,
}: PageArgs<LangParams, SelectHotelSearchParams>) {
  setLang(params.lang)
  const searchDetails = await getHotelSearchDetails({ searchParams })
  if (!searchDetails) return notFound()
  const {
    city,
    urlSearchParams,
    adultsInRoom,
    childrenInRoom,
    childrenInRoomArray,
  } = searchDetails

  if (!city) return notFound()

  const reservationParams = {
    selectHotelParams: urlSearchParams,
    searchParams,
    adultsInRoom,
    childrenInRoom,
    childrenInRoomArray,
  }

  return (
    <Suspense
      key={`${city.name}-${searchParams.fromDate}-${searchParams.toDate}-${adultsInRoom}-${childrenInRoom}`}
      fallback={<SelectHotelSkeleton />}
    >
      <SelectHotel
        city={city}
        params={params}
        reservationParams={reservationParams}
      />
    </Suspense>
  )
}
