import { redirect } from 'next/navigation'
import { getSessionUser } from '@/lib/auth'
import { contextForAccount, dashboardPath } from '@/lib/page-context'

/**
 * `/dashboard` is no longer a page (article "Role-Specific Page Separation",
 * 2026-09-13): every role has its own dashboard route. This resolves the
 * signed-in role — with the internal-only `?view=` override for admin testing
 * — and forwards there, carrying the pilot preview query (`onboarding`, `tab`).
 */
export default async function DashboardRedirect({
  searchParams,
}: {
  searchParams: Promise<{ view?: string; onboarding?: string; tab?: string }>
}) {
  const { view, onboarding, tab } = await searchParams
  const user = await getSessionUser()
  if (!user) redirect('/login')
  const ctx = contextForAccount(user.role, view, user.internal)
  const q = new URLSearchParams()
  if (onboarding) q.set('onboarding', onboarding)
  if (tab) q.set('tab', tab)
  const qs = q.toString()
  redirect(`${dashboardPath(ctx)}${qs ? `?${qs}` : ''}`)
}
