import { auth } from '@/auth'
import { redirect } from 'next/navigation'
import { ProfileForm } from './profile-form'
import prisma from '@/lib/prisma'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Sparkles } from 'lucide-react'
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')
  }

  const user = await prisma.user.findUnique({
    where: { id: session.user.id },
    select: { name: true, email: true, role: true }
  })

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

  // Get user AI settings
  let userAISettings = { preferredLanguage: 'auto', showRecentNotes: false }
  try {
    const aiSettings = await prisma.userAISettings.findUnique({
      where: { userId: session.user.id }
    })

    if (aiSettings) {
      userAISettings = {
        preferredLanguage: aiSettings.preferredLanguage || 'auto',
        showRecentNotes: aiSettings.showRecentNotes ?? false
      }
    }
  } catch (error) {
    console.error('Error fetching AI settings:', error)
  }

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

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