import { awardPoints } from "../../utils/scoring";
import { createChatMessage } from "../../db/chat";

export default defineEventHandler(async (event) => {
  const user = await requireUser(event);
  const body = await readBody(event);
  const content = (body?.content ?? "").trim();

  if (!content)
    throw createError({ statusCode: 400, message: "content erforderlich" });
  if (content.length > 1000)
    throw createError({ statusCode: 400, message: "Nachricht zu lang" });

  const data = await createChatMessage(user.id, content);

  await awardPoints(user.id, "chat_message").catch(() => {});

  return data;
});
