import { PasswordResetForm } from '@/components'
import { SESSION_COOKIE_NAME } from '@/constants'
import { CheckResetPasswordTokenDocument } from '@/generated/graphql/graphql'
import { createApolloClient } from '@/lib'
import { Text } from '@chakra-ui/react'
import { cookies } from 'next/headers'

interface Props {
  params: Promise<{
    token: string
  }>
}

export const dynamic = 'force-dynamic'

const ResetPasswordPage: React.FC<Props> = async ({ params }) => {
  const { token } = await params
  const cookieStore = await cookies()
  const cookie = cookieStore.get(SESSION_COOKIE_NAME)?.value
  const apollo = await createApolloClient(cookie)
  const { data, error } = await apollo.query({ query: CheckResetPasswordTokenDocument, variables: { token } })

  if (error || data === undefined) {
    console.log(error)
    return <Text>An error has occured. Please try again later.</Text>
  }

  else if (!data.checkResetPasswordToken) {
    return <Text>Invalid token!</Text>
  }

  else {
    return <PasswordResetForm token={token}/>
  }
}
export default ResetPasswordPage
