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

export async function onRequestDelete(context: RequestContext) {
  let body: { id?: string } = {};

  try {
    body = await context.request.json();
  } catch {
    return jsonError("Invalid body", 400);
  }

  const { id } = body;

  if (
    typeof id !== "string" ||
    id.match(/\D/) ||
    id.length > 19 ||
    id.length < 17
  )
    return jsonError("Invalid ID", 400);

  await context.env.D1.prepare("DELETE FROM et_members WHERE id = ?;")
    .bind(id)
    .run();

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

export async function onRequestPatch(context: RequestContext) {
  let body: { id?: string; name?: string; roblox_username?: string } = {};

  try {
    body = await context.request.json();
  } catch {
    return jsonError("Invalid body", 400);
  }

  if (typeof body.name !== "string" && typeof body.roblox_username !== "string")
    return jsonError("At least one property must be provided", 400);

  const updates = [];

  if (body.name?.length) updates.push({ query: "name = ?", value: body.name });

  if (typeof body.roblox_username === "string" && body.roblox_username) {
    const robloxResolveResp = await fetch(
      "https://users.roblox.com/v1/usernames/users",
      {
        body: JSON.stringify({
          excludeBannedUsers: true,
          usernames: [body.roblox_username],
        }),
        headers: {
          "content-type": "application/json",
        },
        method: "POST",
      },
    );

    const { data } = (await robloxResolveResp.json()) as {
      data: { [k: string]: any }[];
    };

    if (!data.length)
      return jsonError("No Roblox user exists with that name", 400);

    updates.push({ query: "roblox_id = ?", value: data[0].id });
  }

  await context.env.D1.prepare(
    `UPDATE et_members
     SET ${updates.map((u) => u.query).join(", ")}
     WHERE id = ?;`,
  )
    .bind(...updates.map((u) => u.value), body.id)
    .run();

  return jsonResponse(
    JSON.stringify({
      name: body.name,
      roblox_id: updates.find((u) => typeof u.value === "number")?.value,
    }),
  );
}

export async function onRequestPost(context: RequestContext) {
  const { id, name, roblox_username } = context.data.body;

  if (
    typeof id !== "string" ||
    id.match(/\D/) ||
    id.length > 19 ||
    id.length < 17
  )
    return jsonError("Invalid user ID", 400);

  if (typeof name !== "string" || !name.length || name.length > 32)
    return jsonError("Invalid name", 400);

  if (
    await context.env.D1.prepare("SELECT * FROM et_members WHERE id = ?;")
      .bind(id)
      .first()
  )
    return jsonError("User is already a member", 400);

  if (!["string", "undefined"].includes(typeof roblox_username))
    return jsonError("Roblox username must be a string", 400);

  let roblox_id: number | undefined = undefined;

  if (roblox_username) {
    if (
      roblox_username.length < 3 ||
      roblox_username.length > 20 ||
      roblox_username.match(/\W/) ||
      roblox_username.match(/_/g)?.length > 1
    )
      return jsonError("Username is invalid", 400);

    const usernameResolveResp = await fetch(
      "https://users.roblox.com/v1/usernames/users",
      {
        body: JSON.stringify({
          excludeBannedUsers: true,
          usernames: [roblox_username],
        }),
        headers: {
          "content-type": "application/json",
        },
        method: "POST",
      },
    );

    if (!usernameResolveResp.ok) {
      console.log(await usernameResolveResp.text());
      return jsonError("An error occurred when looking up that username", 500);
    }

    const { data }: { data: { id: number }[] } =
      await usernameResolveResp.json();

    if (!data.length)
      return jsonError(
        "No user was found with that name, either they don't exist or they are banned",
        400,
      );

    roblox_id = data[0].id;
  }

  const createdAt = Date.now();
  const addingUser = context.data.current_user.id;

  await context.env.D1.prepare(
    "INSERT INTO et_members (created_at, created_by, id, name, roblox_id) VALUES (?, ?, ?, ?, ?);",
  )
    .bind(createdAt, addingUser, id, name, roblox_id || null)
    .run();

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