import { prisma } from "./src/constants";

const roles = await prisma.role.findMany({ select: { title: true, moniker: true, permissions: true } });

for (const role of roles) {
  const token = role.permissions;
  if (!token) { console.log(role.title, "- NO PERMISSIONS"); continue; }
  try {
    const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
    const perms: string[] = payload.permissions || [];
    // Check for obj.catalogItem or wildcard that would cover it
    const relevant = perms.filter((p: string) =>
      p.includes("obj.catalogItem") ||
      p.includes("obj.*") ||
      p === "*"
    );
    console.log("=== " + role.title + " (" + role.moniker + ") ===");
    console.log("  obj.catalogItem-related perms:", JSON.stringify(relevant));
    // Also show all perms for debugging
    console.log("  Total perms:", perms.length);
  } catch(e: any) { console.log(role.title, "- parse error:", e.message); }
}

await prisma.$disconnect();
