import { redirect } from "next/navigation";

import { auth } from "@/auth";
import { prisma } from "@/lib/db";
import type { ProfileDTO } from "@/types/profile";
import { ProfileForm } from "@/components/profile/profile-form";

export default async function ProfilePage() {
  const session = await auth();
  // Defense in depth: proxy.ts already redirects unauthenticated requests,
  // but every protected data boundary should check for itself too.
  if (!session?.user) redirect("/login");

  const user = await prisma.user.findUnique({
    where: { id: session.user.id },
    select: { firstName: true, lastName: true, avatar: true },
  });
  if (!user) redirect("/login");

  const profile: ProfileDTO = {
    firstName: user.firstName,
    lastName: user.lastName,
    avatar: user.avatar,
  };

  return (
    <div className="flex h-full flex-col gap-4 p-4">
      <div className="px-1">
        <h1 className="font-heading text-xl font-bold tracking-tight">Profile</h1>
        <p className="mt-0.5 text-[13px] text-muted-foreground">
          Your name and photo, shown in the menu on the left.
        </p>
      </div>

      <ProfileForm initial={profile} />
    </div>
  );
}
