import { usePrisma } from "../../utils/prisma";

export default defineEventHandler(async (event) => {
  const user = await requireUser(event);
  const body = await readBody(event);

  const { gameName, stars, feedback, score } = body;

  if (!gameName || typeof stars !== "number" || stars < 1 || stars > 5) {
    throw createError({ statusCode: 400, message: "Invalid rating data" });
  }

  const db = usePrisma();
  const rating = await db.gameRating.create({
    data: {
      userId: user.id,
      gameName: String(gameName).toLowerCase().slice(0, 50),
      stars,
      feedback: feedback ? String(feedback).slice(0, 500) : null,
      score: typeof score === "number" ? score : 0,
    },
  });

  return { id: rating.id };
});
