import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
import { userService } from "@/services/users/user.service";
import { getBackgroundImage } from "@/lib/preferences";
import NavigationWrapper from "@/components/navigation/NavigationWrapper";
import ProfileForm from "@/components/profile/ProfileForm";

export default async function ProfilePage() {
  const session = await auth();

  if (!session?.user) {
    redirect("/login");
  }

  // Paralléliser les appels DB
  const [user, backgroundImage] = await Promise.all([
    userService.getUserById(session.user.id, {
      id: true,
      email: true,
      username: true,
      avatar: true,
      bio: true,
      characterClass: true,
      hp: true,
      maxHp: true,
      xp: true,
      maxXp: true,
      level: true,
      score: true,
      createdAt: true,
    }),
    getBackgroundImage("home", "/got-background.jpg"),
  ]);

  if (!user) {
    redirect("/login");
  }

  // Convert Date to string for the component
  const userProfile = {
    ...user,
    createdAt: user.createdAt.toISOString(),
  };

  return (
    <main className="min-h-screen bg-black relative">
      <NavigationWrapper />
      <ProfileForm
        initialProfile={userProfile}
        backgroundImage={backgroundImage}
      />
    </main>
  );
}
