import { AppShell } from "@/components/app-shell"
import { translator } from "@/lib/i18n"
import { countPeople, listPeople } from "@/lib/services/people"
import { requireAtLeast } from "@/lib/services/session"
import { PeopleTable } from "./people-table"

/**
 * The roster, and the way into one person.
 *
 * This screen exists because the factory network has no internet and there is
 * no email to send anything with. Everything it does was already possible from
 * a terminal, which meant every lost password was a message to a developer.
 *
 * Admin only, per §8. It hands out passwords and withdraws access.
 */
export default async function Page({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>
}) {
  const user = await requireAtLeast("admin")
  const params = await searchParams
  const query = (typeof params.q === "string" ? params.q : "").trim()

  const [rows, total] = await Promise.all([listPeople(query), countPeople()])
  const t = translator(user.language)

  return (
    <AppShell title="people.title" user={user}>
      <div className="flex flex-col gap-4">
        <p className="text-foreground/80 tabular-nums">
          {t("people.count", { shown: rows.length, total })}
        </p>
        <PeopleTable rows={rows} />
      </div>
    </AppShell>
  )
}
