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

  if (!currentUser.email)
    return new Response(null, {
      status: 204,
    });

  const deliveryDate = new Date(
    Date.now() + 86400000 + Math.round(Math.random() * 172800000),
  )
    .toUTCString()
    .replace("GMT", "+0000");

  const emailData = new FormData();
  emailData.append("from", "totallyrealadminapplication@mail.carcrushers.cc");
  emailData.append("to", currentUser.email);
  emailData.append("subject", "Your admin application has been approved");
  emailData.append("o:deliverytime", deliveryDate);
  emailData.append("v:username", currentUser.username);
  emailData.append("template", "admin_application");

  await fetch("https://api.mailgun.net/v3/mail.carcrushers.cc/messages", {
    body: emailData,
    headers: {
      authorization: `Basic ${btoa(`api:${context.env.MAILGUN_API_KEY}`)}`,
    },
    method: "POST",
  });

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