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

  if (context.data.targetId.search(/^\d{16,19}$/) === -1)
    return new Response('{"error":"Invalid target id"}', {
      headers: {
        "content-type": "application/json",
      },
      status: 400,
    });

  await context.env.DATA.put(
    `appealban_${context.data.targetId}`,
    JSON.stringify({ moderator: currentUser.id })
  );
  await fetch(context.env.APPEALS_WEBHOOK, {
    body: JSON.stringify({
      embeds: [
        {
          title: "User Blocked",
          color: 3756250,
          description: `User ${context.data.targetId} was blocked from the appeal form.`,
          fields: [
            {
              name: "Moderator",
              value: `${currentUser.username}#${currentUser.discriminator} (${currentUser.id})`,
            },
          ],
        },
      ],
    }),
    headers: {
      "content-type": "application/json",
    },
    method: "POST",
  });

  return new Response(null, {
    status: 204,
  });
}
