import { jsonError } from "../../../common.js";

export async function onRequestPost(context: RequestContext) {
  const { content, target } = context.data.body;

  if (typeof content !== "string")
    return jsonError("'content' property is not a string", 400);

  if (typeof target !== "number" || !Number.isSafeInteger(target))
    return jsonError("'target' property is not a valid number", 400);

  if (content.length > 1000)
    return jsonError(
      "'content' property must be less than 1000 characters",
      400,
    );

  const id = `${Date.now()}${crypto.randomUUID().replaceAll("-", "")}`;

  await context.env.D1.prepare(
    "INSERT INTO game_mod_notes (content, created_at, created_by, id, target) VALUES (?, ?, ?, ?, ?);",
  )
    .bind(content, Date.now(), context.data.current_user.id, id, target)
    .first();
}
