import { cache } from 'react'
import { prisma } from '@/lib/prisma'

const accessFilter = (userId: string) => ({
  OR: [
    { user_id: userId },
    { members: { some: { user_id: userId } } },
  ],
})

export const getAccessibleProduct = cache(async (productId: string, userId: string) => {
  return prisma.product.findFirst({
    where: { id: productId, ...accessFilter(userId) },
  })
})

export function productAccessFilter(userId: string) {
  return accessFilter(userId)
}
