import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";

export async function GET() {
  try {
    const users = await prisma.user.findMany({
      orderBy: {
        score: "desc",
      },
      take: 10,
      select: {
        id: true,
        username: true,
        score: true,
        level: true,
        avatar: true,
      },
    });

    const leaderboard = users.map((user: { id: string; username: string; score: number; level: number; avatar: string | null }, index: number) => ({
      rank: index + 1,
      username: user.username,
      score: user.score,
      level: user.level,
      avatar: user.avatar,
    }));

    return NextResponse.json(leaderboard);
  } catch (error) {
    console.error("Error fetching leaderboard:", error);
    return NextResponse.json(
      { error: "Erreur lors de la récupération du leaderboard" },
      { status: 500 }
    );
  }
}

