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

/**
 * `/trips/new` is no longer a page for brokers, carriers or pilot dispatch —
 * each has its own New Trip route (Nash, 2026-09-13: "depending from which
 * dashboard you click on the button, the right link should open"):
 *   freight broker  → /fb-new-trip  (or /fb-new-trip-request without ?mode=trip)
 *   carrier dispatch → /cd-new-trip
 *   pilot dispatch  → /pd-new-trip
 * Old links keep working through this redirect.
 */
export default async function NewTripRedirect({
  searchParams,
}: {
  searchParams: Promise<{ mode?: string }>
}) {
  const user = await getSessionUser()
  if (!user) redirect('/login?next=%2Ftrips%2Fnew')
  const { mode } = await searchParams
  const ctx = contextForAccount(user.role)
  if (ctx === 'carrier') redirect('/cd-new-trip')
  if (ctx === 'pilot-company') redirect('/pd-new-trip')
  redirect(mode === 'trip' ? '/fb-new-trip' : '/fb-new-trip-request')
}
