import { type Metadata } from 'next'
import { notFound, redirect } from 'next/navigation'

import { auth } from '@/auth'
import { getChat } from '@/app/actions'
import { Chat } from '@/components/chat'
import { ChatModel } from '@/components/chat-model'

export interface ChatPageProps {
  params: {
    id: string
    locale: string
  }
}


export async function generateMetadata({
  params
}: ChatPageProps): Promise<Metadata> {
  const session = await auth()

  if (!session?.user?.id) {
    return {}
  }

  const chat = await getChat(params.id, session?.user?.id)
  return {
    title: chat?.title.toString().slice(0, 50) ?? 'Chat'
  }
}

export default async function ChatPage({ params }: ChatPageProps) {

  const session = await auth()
  const avatarUrl = session?.user?.image

  const serial_no = process.env.NEXT_PUBLIC_STAFF_SERIAL_NO || ""

  // if (!session?.user?.id) {
  //   // redirect(`/auth/sign-in?next=/chat/${params.id}`)
  //   // redirect(`/${params.locale}/chat/${params.id}`)
  //   redirect(`/`)
  // }

  // const chat = await getChat(params.id, session.user.id)

  // if (!chat) {
  //   notFound()
  // }

  // if (chat?.userId !== session?.user?.id) {
  //   notFound()
  // }

  // return <Chat showLanding id={chat.id} initialMessages={chat.messages} avatarUrl={avatarUrl} />
  return <ChatModel id={params.id} showLanding avatarUrl={avatarUrl} serial_no={serial_no} />
}
