import SharePage from './share-page'
import { HydrationBoundary } from '@tanstack/react-query'
import { dehydrate } from '@tanstack/react-query'
import { QueryClient } from '@tanstack/react-query'
import { aiUserKeys } from '@/lib/query-keys'
import { userService } from '@/services/user'
import { ApiError } from '@/types/api'
import { notFound } from 'next/navigation'

const Page = async ({ params }: { params: Promise<{ userId?: string }> }) => {
  const { userId } = await params

  if (!userId) {
    notFound()
  }

  const queryClient = new QueryClient()

  try {
    // 预获取用户基本信息
    await queryClient.fetchQuery({
      queryKey: aiUserKeys.baseInfo({ aiId: Number(userId) }),
      queryFn: () => userService.getAIUserBaseInfo({ aiId: Number(userId) }),
    })
  } catch (error) {
    if (error instanceof ApiError && error.errorCode === '10010012') {
      notFound()
    }
    // 其他错误不影响页面渲染，让客户端处理
  }

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <SharePage />
    </HydrationBoundary>
  )
}

export default Page
