import { AppShell } from '@/components/app/app-shell'
import { ViewSwitcher } from '@/components/app/view-switcher'
import { isRoleSwitchEnabled } from '@/lib/auth/env-users'
import { requireRoleRoute } from '@/lib/role-route'
import { loadDashboardBase, loadDriverDashboard } from '@/lib/data/dashboard'
import { resolveLanguagePrefs } from '@/lib/domain/languages'
import { DriverView } from './driver-view'

/** Carrier Driver App / Dashboard (CT) — the truck driver. */
export default async function CarrierDriverDashboardPage() {
  const { user, profile } = await requireRoleRoute('carrier-driver')
  const base = await loadDashboardBase(user)
  const extra = await loadDriverDashboard(user, base)
  const languagePrefs = resolveLanguagePrefs(profile)
  return (
    <AppShell profile={profile}>
      {user.internal && !isRoleSwitchEnabled() && <ViewSwitcher current="carrier-driver" />}
      <DriverView
        trips={base.trips}
        warnings={base.warnings}
        permits={base.permits}
        userId={user.id}
        accountCreatedAt={profile.created_at}
        isInternal={user.internal}
        myName={profile.full_name}
        routeRequests={extra.routeRequests}
        units={extra.units}
        permitUrls={extra.permitUrls}
        chat={extra.chat}
        participants={extra.participants}
        myRole={extra.myRole}
        completions={base.completions}
        shareChat={extra.shareChat}
        // Nash: "By default, the driver dashboard should use the My Trips as
        // the landing." A tapped trip opens its own trip workspace on Active Trip.
        landingPane="my-trips"
        routeCredits={extra.routeCredits}
        chatLanguages={languagePrefs.languages}
        primaryLanguage={languagePrefs.primary}
        pilotAccess={extra.pilotAccess}
      />
    </AppShell>
  )
}
