import { jsonError } from "../../../common.js";
import { getBanList } from "../../../roblox-open-cloud.js";

export async function onRequestPost(context: RequestContext) {
  const appealId = context.params.id as string;
  const appeal = await context.data.prisma.gameAppeal.findUnique({
    select: {
      roblox_id: true,
      type: true,
    },
    where: {
      id: appealId,
    },
  });

  if (!appeal) return jsonError("Appeal not found", 404);

  await context.data.prisma.gameAppeal.delete({
    where: {
      id: appealId,
    },
  });

  const { value: banList } = await getBanList(context);

  await context.data.prisma.gameModLog.create({
    data: {
      action: `deny appeal | ${banList[appeal.roblox_id]?.BanType === 2 ? "ban" : appeal.type}`,
      evidence: `https://carcrushers.cc/mod-queue?id=${context.params.id}&type=gma`,
      executor: context.data.current_user.id,
      id: crypto.randomUUID(),
      target: appeal.roblox_id,
    },
  });

  await context.env.DATA.put(
    `gameappealblock_${appeal.roblox_id}`,
    `${Date.now() + 2592000000}`,
    { expirationTtl: 2592000 },
  );

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