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

async function findSeller(where, options = {}) {
  return prisma.seller.findFirst({
    where,
    include: options.include || undefined,
    select: options.select || undefined,
  })
}

async function findSellerByDomain(domain) {
  return prisma.seller.findFirst({
    where: {
      domains: {
        some: {
          domain: domain,
        },
      },
    },
  })
}

async function findSellers(where = {}, options = {}) {
  return prisma.seller.findMany({
    where,
    include: options.include || undefined,
    select: options.select || undefined,
    orderBy: options.orderBy || { name: "asc" },
  })
}

async function createSeller(data) {
  return prisma.seller.create({ data })
}

async function updateSeller(id, data) {
  return prisma.seller.update({
    where: { id },
    data,
  })
}

module.exports = {
  findSeller,
  findSellerByDomain,
  findSellers,
  createSeller,
  updateSeller,
}
