export async function onRequestPost(context: RequestContext) {
  const { permissions } = context.data.current_user;

  if (!(permissions & (1 << 0)) || !(permissions & (1 << 11)))
    return new Response('{"error":"Forbidden"}', {
      headers: {
        "content-type": "application/json",
      },
      status: 403,
    });

  const { body } = context.data;
  const id = context.params.id as string;

  context.data.targetId = id;

  if (!new URL(context.request.url).pathname.endsWith("/ban")) {
    const keyWithMeta = await context.env.DATA.getWithMetadata(`appeal_${id}`);

    if (!keyWithMeta.value)
      return new Response('{"error":"No appeal with that ID exists"}', {
        headers: {
          "content-type": "application/json",
        },
        status: 404,
      });

    context.data.appeal = keyWithMeta;
  }

  if (
    body.feedback &&
    (typeof body.feedback !== "string" || body.feedback.length > 512)
  )
    return new Response('{"error":"Invalid feedback"}', {
      headers: {
        "content-type": "application/json",
      },
      status: 400,
    });

  return await context.next();
}