import { redirect } from "next/navigation"
import React from "react"

import { getProfileSafely } from "@/lib/trpc/memoizedRequests"

import { AlreadyLinkedError } from "../components/AlreadyLinkedError"
import { SASModal } from "../components/SASModal"
import { LinkAccountForm } from "./LinkAccountForm"

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

export default async function SASxScandicLinkPage({
  params,
}: PageArgs<LangParams>) {
  const profile = await getProfileSafely()

  // TODO check if already linked
  const alreadyLinked = false

  if (alreadyLinked) {
    redirect(`/${params.lang}/sas-x-scandic/error?errorCode=alreadyLinked`)
  }

  return (
    <SASModal>
      <LinkAccountForm userDateOfBirth={profile?.dateOfBirth ?? null} />
    </SASModal>
  )
}
