import { PaymentCallbackPage as PaymentCallbackPagePrimitive } from "@scandic-hotels/booking-flow/pages/PaymentCallbackPage"
import { logger } from "@scandic-hotels/common/logger"
import { TrackingSDK } from "@scandic-hotels/tracking/TrackingSDK"
import {
  TrackingChannelEnum,
  type TrackingSDKPageData,
} from "@scandic-hotels/tracking/types"

import { bookingFlowConfig } from "@/constants/bookingFlowConfig"

import { getSocialSession, isValidSocialSession } from "@/auth/scandic/session"

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

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

export default async function PaymentCallbackPage(
  props: PageArgs<LangParams & { status: string }>
) {
  const searchParams = await props.searchParams
  const params = await props.params
  logger.debug(`[payment-callback] callback started`)
  const lang = params.lang

  let userAccessToken = null
  const session = await getSocialSession()
  if (isValidSocialSession(session)) {
    userAccessToken = session.access_token
  }

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

  return (
    <>
      <TrackingSDK pageData={pageTrackingData} />
      <PaymentCallbackPagePrimitive
        lang={lang}
        userAccessToken={userAccessToken}
        searchParams={searchParams}
        status={params.status as PaymentCallbackStatusEnum}
        config={bookingFlowConfig}
      />
    </>
  )
}
