import { Player } from "@prisma/client"
import bcrypt from "bcrypt"

export default async function checkPasswordIsValid<T>(
  payload: T & { player: Player; password: string }
) {
  const { player, password } = payload

  // Validate for correct password
  return bcrypt.compare(password, player.passwordHash).then(async (result) => {
    if (!result) {
      return Promise.reject({
        message: "Passwords do not match!",
        statusCode: 401,
        solved: true,
      })
    }
    return {
      ...payload,
      passwordIsValid: true,
    }
  })
}
