import { auth } from '@/auth'
import { redirect } from 'next/navigation'
import { ProfileForm } from './profile-form'
import prisma from '@/lib/prisma'
import { ProfilePageHeader } from '@/components/profile-page-header'
import { AISettingsLinkCard } from './ai-settings-link-card'

export default async function ProfilePage() {
  const session = await auth()

  if (!session?.user?.id) {
    redirect('/login')
  }

  // Parallel queries
  const [user, aiSettings] = await Promise.all([
    prisma.user.findUnique({
      where: { id: session.user.id },
      select: { name: true, email: true, role: true }
    }),
    prisma.userAISettings.findUnique({
      where: { userId: session.user.id }
    })
  ])

  if (!user) {
    redirect('/login')
  }

  const userAISettings = {
    preferredLanguage: aiSettings?.preferredLanguage || 'auto',
    showRecentNotes: aiSettings?.showRecentNotes ?? false
  }

  return (
    <div className="max-w-2xl">
      <ProfilePageHeader />
      <ProfileForm user={user} userAISettings={userAISettings} />

      {/* AI Settings Link */}
      <AISettingsLinkCard />
    </div>
  )
}
