import { notFound } from "next/navigation"

import { logger } from "@scandic-hotels/common/logger"
import { TrackingSDK } from "@scandic-hotels/tracking/TrackingSDK"
import {
  TrackingChannelEnum,
  type TrackingSDKPageData,
} from "@scandic-hotels/tracking/types"

import { myStay } from "@/constants/routes/webviews"

import GuaranteeCallbackPage from "@/components/GuaranteeCallback"

import type { Lang } from "@scandic-hotels/common/constants/language"
import type { PaymentCallbackStatusEnum } from "@scandic-hotels/common/constants/paymentCallbackStatusEnum"

export default async function GuaranteePaymentWebViewCallbackPage(
  props: PageProps<"/[lang]/webview/hotelreservation/gla-payment-callback">
) {
  const searchParams = await props.searchParams
  const params = await props.params
  const lang = params.lang as Lang
  const status = searchParams.status as PaymentCallbackStatusEnum
  const confirmationNumber =
    typeof searchParams.confirmationNumber === "string"
      ? searchParams.confirmationNumber
      : undefined
  const refId =
    typeof searchParams.RefId === "string" ? searchParams.RefId : undefined

  if (!status || !confirmationNumber || !refId) {
    notFound()
  }
  const myStayUrl = `${myStay[lang]}?RefId=${encodeURIComponent(refId)}`

  const pageTrackingData: TrackingSDKPageData = {
    pageId: "webview|gla-payment-callback",
    domainLanguage: lang,
    channel: TrackingChannelEnum["hotelreservation"],
    pageName: "webview|gla-payment-callback",
    siteSections: "webview|hotelreservation|gla-payment-callback",
    pageType: "hotelreservationsgla-payment-callback",
    siteVersion: "new-web",
  }

  logger.debug(`[gla-payment-callback] callback started`)
  return (
    <>
      <TrackingSDK pageData={pageTrackingData} />
      <GuaranteeCallbackPage
        status={status}
        confirmationNumber={confirmationNumber}
        refId={refId}
        myStayUrl={myStayUrl}
        lang={lang}
        isAncillaryFlow={!!searchParams.ancillary}
      />
    </>
  )
}
