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

export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig();
  const secret = getHeader(event, "x-admin-secret");
  if (!config.adminSecret || secret !== config.adminSecret) {
    throw createError({ statusCode: 401, message: "Nicht autorisiert" });
  }

  const id = getRouterParam(event, "id");
  if (!id) throw createError({ statusCode: 400, message: "id fehlt" });

  const body = await readBody(event) as {
    status?: string;
    adminNote?: string;
    category?: string;
  };

  const db = usePrisma();
  const updated = await db.feedbackItem.update({
    where: { id },
    data: {
      ...(body.status !== undefined && { status: body.status as any }),
      ...(body.adminNote !== undefined && { adminNote: body.adminNote }),
      ...(body.category !== undefined && { category: body.category }),
    },
  });

  return updated;
});
