import type { Metadata } from 'next'
import { aiUserKeys } from '@/lib/query-keys'
import ChatPage from './chat-page'
import { userService } from '@/services/user'
import { QueryClient } from '@tanstack/react-query'
import { ApiError } from '@/types/api'
import { notFound } from 'next/navigation'

export const generateMetadata = async ({ params }: { params: Promise<{ aiId: string }> }) => {
  const { aiId } = await params
  try {
    const resp = await userService.getSEOUserBaseInfo({ aiId: Number(aiId) })
    const { nickname, homeImageUrl } = resp || {}

    return {
      title: `Chat with ${nickname} - CrushLevel AI`,
      description: `Chat with ${nickname} on CrushLevel AI. Start your love story and build meaningful conversations with AI companions.`,
      keywords: [
        'CrushLevel chat',
        'AI chat',
        'chat with AI',
        nickname || 'AI companion',
        'AI conversation',
        'virtual chat',
        'AI girlfriend chat',
        'AI boyfriend chat',
      ],
      openGraph: {
        title: `Chat with ${nickname} - CrushLevel AI`,
        description: `Chat with ${nickname} on CrushLevel AI. Start your love story and build meaningful conversations with AI companions.`,
        images: {
          url: homeImageUrl,
          width: 720,
          height: 1280,
          alt: `${nickname} - CrushLevel AI`,
        },
        siteName: 'CrushLevel AI',
        type: 'website',
        url: `https://www.crushlevel.com/chat/${aiId}`,
      },
      twitter: {
        card: 'summary_large_image',
        title: `Chat with ${nickname} - CrushLevel AI`,
        description: `Chat with ${nickname} on CrushLevel AI. Start your love story and build meaningful conversations.`,
        images: {
          url: homeImageUrl,
          width: 720,
          height: 1280,
          alt: `${nickname} - CrushLevel AI`,
        },
      },
      robots: {
        index: false,
        follow: false,
      },
      alternates: {
        canonical: `https://www.crushlevel.com/chat/${aiId}`,
      },
    }
  } catch (error) {
    return {
      title: 'Chat - CrushLevel AI',
      description:
        'Chat with AI companions on CrushLevel AI. Start your love story and build meaningful conversations.',
      openGraph: {
        title: 'Chat - CrushLevel AI',
        description:
          'Chat with AI companions on CrushLevel AI. Start your love story and build meaningful conversations.',
        url: `https://www.crushlevel.com/chat/${aiId}`,
        siteName: 'CrushLevel AI',
        type: 'website',
      },
      robots: {
        index: false,
        follow: false,
      },
    }
  }
}

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

  const queryClient = new QueryClient()

  // try {
  //   // 使用 fetchQuery 替代 prefetchQuery，因为 fetchQuery 会抛出错误
  //   await queryClient.fetchQuery({
  //     queryKey: aiUserKeys.tempBaseInfo({ aiId: Number(aiId) }),
  //     queryFn: () => userService.getAIUserBaseInfo({ aiId: Number(aiId) }),
  //   });
  // } catch (error) {
  //   if (error instanceof ApiError && error.errorCode === "10010012") {
  //     notFound();
  //   }
  // }

  return <ChatPage />
}

export default Page
