import type { PrismaClient } from "@capakraken/db";

export interface GetDashboardTopValueResourcesInput {
  limit: number;
  userRole: string;
}

export async function getDashboardTopValueResources(
  db: PrismaClient,
  input: GetDashboardTopValueResourcesInput,
) {
  const settings = await db.systemSettings.findUnique({
    where: { id: "singleton" },
  });

  const visibleRoles =
    (settings?.scoreVisibleRoles as unknown as string[]) ?? ["ADMIN", "MANAGER"];

  if (!visibleRoles.includes(input.userRole)) {
    return [];
  }

  return db.resource.findMany({
    where: { isActive: true, valueScore: { not: null } },
    select: {
      id: true,
      eid: true,
      displayName: true,
      chapter: true,
      valueScore: true,
      lcrCents: true,
    },
    orderBy: { valueScore: "desc" },
    take: input.limit,
  });
}
