import { prisma } from "@/lib/db";

export async function canAccessEvaluation(
  evaluationId: string,
  userId: string,
  isAdmin: boolean,
  readOnly = false
) {
  if (isAdmin) return true;
  const eval_ = await prisma.evaluation.findUnique({
    where: { id: evaluationId },
    select: { evaluatorId: true, isPublic: true, sharedWith: { select: { userId: true } } },
  });
  if (!eval_) return false;
  if (eval_.evaluatorId === userId) return true;
  if (eval_.sharedWith.some((s) => s.userId === userId)) return true;
  if (readOnly && eval_.isPublic) return true;
  return false;
}
