import { redirect } from 'next/navigation'
import { prisma } from '@/lib/prisma'
import { requireRole } from '@/lib/auth-redirect'
import { ApplicantNav } from '@/components/layouts/applicant-nav'

export const dynamic = 'force-dynamic'

export default async function ApplicantLayout({
  children,
}: {
  children: React.ReactNode
}) {
  const session = await requireRole('APPLICANT')

  // Check if user has completed onboarding
  const user = await prisma.user.findUnique({
    where: { id: session.user.id },
    select: { onboardingCompletedAt: true },
  })

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

  if (!user.onboardingCompletedAt) {
    redirect('/onboarding')
  }

  return (
    <div className="min-h-screen bg-background">
      <ApplicantNav
        user={{
          name: session.user.name,
          email: session.user.email,
        }}
      />
      <main className="container-app py-6 lg:py-8">{children}</main>
    </div>
  )
}
