import { redirect } from 'next/navigation';
import { getTranslations } from 'next-intl/server';
import { prisma } from '@repo/db';
import { resolveUser } from '@/lib/auth';
import { OperatorPicker } from './operator-picker';

// This page intentionally fetches operators without a session: the picker IS
// the login step. prisma is used directly (bypassing the tRPC auth layer) so
// the page works even when AUTH_DEV_AUTOLOGIN=false.
export default async function SelectOperatorPage() {
  const t = await getTranslations('auth');
  const user = await resolveUser();
  if (user) redirect('/');

  const operators = await prisma.user.findMany({
    where: { role: 'OPERATOR' },
    select: { id: true, email: true },
    orderBy: { email: 'asc' },
  });

  return (
    <main className="mx-auto flex min-h-screen max-w-sm flex-col justify-center gap-8 p-6">
      <header className="text-center">
        <h1 className="text-2xl font-bold tracking-tight">{t('pickerTitle')}</h1>
        <p className="mt-1 text-sm text-muted-foreground">{t('pickerSubtitle')}</p>
      </header>
      <OperatorPicker operators={operators} />
    </main>
  );
}
