import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { MobileEntry } from "@/app/mobile/MobileEntry";

export const dynamic = "force-dynamic";

export default async function MobilePage() {
  const session = await auth();
  if (!session?.user?.id) redirect("/auth/login");
  const userId = session.user.id;

  const [patient, last3] = await Promise.all([
    prisma.patient.findUnique({ where: { userId }, select: { firstName: true } }),
    prisma.reading.findMany({
      where: { userId },
      orderBy: { measuredAt: "desc" },
      take: 3,
      select: { measuredAt: true, moment: true, value: true },
    }),
  ]);

  return (
    <MobileEntry
      firstName={patient?.firstName ?? null}
      last3={last3.map((r) => ({ measuredAt: r.measuredAt.toISOString(), moment: r.moment, value: r.value }))}
    />
  );
}
