import { auth } from "@/lib/auth";
import { userService } from "@/services/users/user.service";
import { challengeService } from "@/services/challenges/challenge.service";
import { houseService } from "@/services/houses/house.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;
  let pendingHouseActionsCount = 0;

  if (session?.user?.id) {
    // Paralléliser les appels DB
    const [user, challengesCount, houseActionsCount] = 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),
      houseService.getPendingHouseActionsCount(session.user.id),
    ]);

    if (user) {
      userData = user;
    }

    activeChallengesCount = challengesCount;
    pendingHouseActionsCount = houseActionsCount;
  }

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