import { auth } from "@/lib/auth";
import { userService } from "@/services/users/user.service";
import { challengeService } from "@/services/challenges/challenge.service";
import Navigation from "./Navigation";

interface UserData {
  username: string;
  avatar: string | null;
  hp: number;
  maxHp: number;
  xp: number;
  maxXp: number;
  level: number;
  score: number;
}

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

  let userData: UserData | null = null;
  const isAdmin = session?.user?.role === "ADMIN";
  let activeChallengesCount = 0;

  if (session?.user?.id) {
    // Paralléliser les appels DB
    const [user, count] = await Promise.all([
      userService.getUserById(session.user.id, {
        username: true,
        avatar: true,
        hp: true,
        maxHp: true,
        xp: true,
        maxXp: true,
        level: true,
        score: true,
      }),
      challengeService.getActiveChallengesCount(session.user.id),
    ]);

    if (user) {
      userData = user;
    }

    activeChallengesCount = count;
  }

  return (
    <Navigation
      initialUserData={userData}
      initialIsAdmin={isAdmin}
      initialActiveChallengesCount={activeChallengesCount}
    />
  );
}
