const { PrismaClient } = require("@prisma/client")
const prisma = new PrismaClient()

async function findUser(where, options = {}) {
  return prisma.user.findUnique({
    where,
    include: options.include || undefined,
    select: options.select || undefined,
  })
}

async function updateUser(where, data, options = {}) {
  return prisma.user.update({
    where,
    data,
    include: options.include || undefined,
    select: options.select || undefined,
  })
}

async function findUsersByIds(ids = [], options = {}) {
  const normalized = Array.from(
    new Set((Array.isArray(ids) ? ids : []).map((id) => Number(id)).filter((id) => Number.isInteger(id) && id > 0))
  )
  if (!normalized.length) return []
  return prisma.user.findMany({
    where: { id: { in: normalized } },
    include: options.include || undefined,
    select: options.select || undefined,
  })
}

module.exports = {
  findUser,
  updateUser,
  findUsersByIds,
}
