import { db } from "./db"
import { ActionType } from "@/lib/shared/constants/modules"

export async function hasPermission(
  roleIds: string[],
  moduleId: string,
  action: ActionType
): Promise<boolean> {
  if (roleIds.length === 0) return false

  const perms = await db.permission.findMany({
    where: {
      roleId: { in: roleIds },
      module: moduleId,
    },
    select: {
      actions: true,
    },
  })

  return perms.some((p) => p.actions.includes(action))
}