import prisma from "../../prisma"
import { rejectionErrors } from "../errors"
import sendError, { API } from "./sendError"
import type { Player, Prisma } from "@prisma/client"

async function updatePlayerDB<T>(
  context: API<T>,
  player: Player,
  data: Prisma.PlayerUpdateInput
) {
  if (!player.anonymous) throw sendError(context, rejectionErrors.registered)
  const updatedPlayer = await prisma.player.update({
    where: { id: player.id },
    data,
  })

  return updatedPlayer
}

export default updatePlayerDB
