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/NavigationWrapper";
import ProfileForm from "@/components/ProfileForm";

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

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

  const user = await 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,
  });

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

  const backgroundImage = await getBackgroundImage(
    "home",
    "/got-background.jpg"
  );

  // 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>
  );
}
