import { UserRole } from "@/generated/prisma/client"
import prisma from "@/lib/prisma"
import { getPasswordHash } from "@/lib/security"
import { User } from "@/lib/types/user"

export async function createUser({ data }: { data: User }) {
  const newUser = await prisma.user.create({
    data: {
      username: data.username,
      name: data.name,
      email: data.email,
      password: await getPasswordHash(data.password),
      role: data.role ?? UserRole.STAFF,
      isActive: data.isActive ?? true,
    },
  })
  return newUser
}

export async function getUserById(id: string) {
  return await prisma.user.findUnique({ where: { id } })
}

export async function getUserByUsername(username: string) {
  return await prisma.user.findUnique({ where: { username } })
}
