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

export default async function getPlayerByIdDB<T>(
  context: API<T>,
  tokenDB: Token
) {
  // Find Host in DB if it still exists (just to make sure)
  const player = await prisma.player.findUnique({
    where: {
      id: tokenDB.ownerId,
    },
  })
  if (!player) {
    throw sendError(context, rejectionErrors.playerNotFound)
  }

  return player
}
