import { auth } from '@/auth'
import { db } from '@/lib/db'
import { users } from '@/lib/db/schema'
import { eq } from 'drizzle-orm'
import { redirect } from 'next/navigation'
import { DashboardSidebar } from '@/components/dashboard/sidebar'
import { DashboardHeader } from '@/components/dashboard/header'

export default async function DashboardLayout({
  children,
}: {
  children: React.ReactNode
}) {
  const session = await auth()
  if (!session?.user?.id) redirect('/auth/login')

  const result = await db
    .select()
    .from(users)
    .where(eq(users.id, session.user.id))
    .limit(1)

  const user = result[0] ?? null

  return (
    <div className="flex min-h-svh">
      <DashboardSidebar user={session.user} profile={user} />
      <div className="flex-1 flex flex-col">
        <DashboardHeader user={session.user} profile={user} />
        <main className="flex-1 p-6 bg-background overflow-auto">
          {children}
        </main>
      </div>
    </div>
  )
}
