import { AlternativeHotelsPage as AlternativeHotelsPagePrimitive } from "@scandic-hotels/booking-flow/pages/AlternativeHotelsPage"

import { bookingFlowConfig } from "@/constants/bookingFlowConfig"
import { getHotel } from "@/lib/trpc/memoizedRequests/getHotel"

import { getIntl } from "@/i18n"
import { getLang } from "@/i18n/serverContext"

import type { Metadata } from "next"

import { type LangParams, type PageArgs } from "@/types/params"

export async function generateMetadata({
  searchParams,
  params,
}: PageArgs<LangParams>): Promise<Metadata> {
  const intl = await getIntl()

  const { hotel } = await searchParams
  const { lang } = await params

  if (!hotel || Array.isArray(hotel)) {
    return {}
  }

  const hotelData = await getHotel({
    hotelId: hotel,
    language: lang,
    isCardOnlyPayment: false,
  })
  const hotelName = hotelData?.additionalData?.name

  if (!hotelName) {
    return {}
  }

  const title = intl.formatMessage(
    {
      defaultMessage: "Alternatives for {value}",
    },
    {
      value: hotelName,
    }
  )

  return { title }
}

export default async function AlternativeHotelsPage(
  props: PageArgs<LangParams>
) {
  const searchParams = await props.searchParams
  const lang = await getLang()

  return (
    <AlternativeHotelsPagePrimitive
      lang={lang}
      searchParams={searchParams}
      config={bookingFlowConfig}
    />
  )
}
