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 { getRouteCreditBalance } from '@/lib/data/credits'
import { loadRoutePurchases } from '@/lib/data/billing'
import { resolveLanguagePrefs } from '@/lib/domain/languages'
import { PilotCompanyDashboard } from './pilot-company-dashboard'

/**
 * Pilot Company / Pilot Dispatch Dashboard (PD). Phase 1 replica on demo
 * data — internal admins only until pilot account roles exist. Billing / Plan
 * and Settings render inside it with the same data /billing and /settings load.
 */
export default async function PilotCompanyDashboardPage({
  searchParams,
}: {
  searchParams: Promise<{ onboarding?: string; tab?: string }>
}) {
  const { user, profile } = await requireRoleRoute('pilot-company')
  const { onboarding, tab } = await searchParams
  const prefs = resolveLanguagePrefs(profile)
  const [routePurchases, routeCredits] = await Promise.all([loadRoutePurchases(user), getRouteCreditBalance(user)])
  return (
    <AppShell profile={profile}>
      {user.internal && !isRoleSwitchEnabled() && <ViewSwitcher current="pilot-company" />}
      <PilotCompanyDashboard
        fresh={onboarding === 'new'}
        initialTab={tab}
        billing={{ routePurchases, routeCredits, internal: user.internal }}
        settings={{ userId: user.id, languages: prefs.languages, primary: prefs.primary }}
      />
    </AppShell>
  )
}
