import { getSessionUser } from '@/lib/auth'
import { findEnvUserByRole, isRoleSwitchEnabled, ROLE_SWITCH_LABELS } from '@/lib/auth/env-users'
import type { UserRole } from '@/types/db'
import { RoleSwitcherBar, type RoleSwitcherItem } from './role-switcher-bar'

/**
 * Pilot testing bar (AUTH_ROLE_SWITCH=true): one sign-in, then jump between
 * every interface from the top of every page.
 *
 * INTERNAL ONLY (Task 96, 2026-09-09). Nash: "for pilot testing, we have like
 * a small bar pilot testing bar. That one should be visible only for admin and
 * developer roles. No other users should see it."
 *
 * Grouped per product (Nash, 2026-09-12): Freight Broker · Carrier · Pilot Car
 * · Admin. A role that has a configured test account switches the REAL session
 * (a form → server action). A role without one — Broker, since the only broker
 * login became admin on 2026-09-11 — falls back to its dashboard route, which
 * an admin may open. Pilot car screens are Phase 1 replicas, always links.
 *
 * The highlight follows the page you are on (Nash, 2026-09-13) — see
 * RoleSwitcherBar. Gated on the session's `internal` flag rather than the
 * current role, because switching into the driver test account changes the
 * role — an admin testing the driver view must keep the bar that brings them
 * back.
 */
export async function RoleSwitcher({ currentRole }: { currentRole: string }) {
  const user = await getSessionUser()
  if (!user?.internal) return null
  if (!isRoleSwitchEnabled()) return null

  const groups = [
    { title: 'Freight Broker', items: [accountOrView('broker', 'Broker dashboard', '/fb-dashboard', ['/fb-dashboard', '/fb-trip-workspace'])] },
    {
      title: 'Carrier',
      items: [
        accountOrView('dispatcher', 'Carrier dashboard', '/cd-dashboard', ['/cd-dashboard', '/cd-trip-workspace']),
        accountOrView('driver', 'Driver app', '/ct-dashboard', ['/ct-dashboard', '/ct-trip-workspace']),
      ],
    },
    {
      title: 'Pilot Car',
      items: [
        { kind: 'link', href: '/signup/pilot', label: 'Signup', activeOn: ['/signup/pilot'] },
        { kind: 'link', href: '/pd-dashboard', label: 'Pilot dispatch dashboard', activeOn: ['/pd-dashboard', '/pd-trip-workspace'] },
        { kind: 'link', href: '/pc-dashboard', label: 'Pilot driver app', activeOn: ['/pc-dashboard', '/pc-trip-workspace'] },
        { kind: 'link', href: '/admin/pilot-preview/HH-2041?as=carrier', label: 'My Pilot tab · carrier', activeOn: ['/admin/pilot-preview'] },
      ] as RoleSwitcherItem[],
    },
    { title: 'Admin', items: [accountOrView('admin', ROLE_SWITCH_LABELS.admin, '/admin/users', ['/admin/users', '/admin/moderation', '/admin/email-templates', '/admin/company-review'])] },
  ]

  return <RoleSwitcherBar groups={groups} currentRole={currentRole} />
}

/** Real session switch when a test account exists for the role; the role's dashboard route otherwise. */
function accountOrView(role: UserRole, label: string, href: string, activeOn: string[]): RoleSwitcherItem {
  if (findEnvUserByRole(role)) return { kind: 'switch', role, label, activeOn }
  return {
    kind: 'link',
    href,
    label,
    activeOn,
    hint: `No ${role} test account in AUTH_USERS — opens the ${role} pages as your current account`,
  }
}
