import { createFileRoute, useNavigate } from '@tanstack/react-router';
import { z } from 'zod';
import { LoginPage } from '@/ui/pages/login';

const LoginSearchSchema = z.object({
  /** Where to bounce the user once login succeeds. Defaults to `/`. */
  redirect: z.string().optional(),
});

export const Route = createFileRoute('/login')({
  component: LoginRoute,
  validateSearch: LoginSearchSchema,
});

function LoginRoute() {
  const { redirect } = Route.useSearch();
  const navigate = useNavigate();

  return (
    <LoginPage
      onAuthenticated={() => {
        void navigate({ to: redirect ?? '/', replace: true });
      }}
    />
  );
}
