import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { getUserIdFromRequest } from "@/lib/auth";

export async function GET(req: Request) {
  const userId = getUserIdFromRequest(req);
  if (!userId) return NextResponse.json({ permissions: [] });
  const roles = await prisma.userRole.findMany({ where: { userId }, select: { roleId: true } });
  if (roles.length === 0) return NextResponse.json({ permissions: [] });
  const roleIds = roles.map((r) => r.roleId);
  const permissions = await prisma.rolePermission.findMany({
    where: { roleId: { in: roleIds }, allowed: true },
    select: { resource: true, action: true },
  });
  return NextResponse.json({ permissions });
}


