import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  console.log('Removing developer:access permission from Admin role...');

  // Find the developer:access permission
  const developerPerm = await prisma.permission.findFirst({
    where: { resource: 'developer', action: 'access' },
  });

  if (!developerPerm) {
    console.log('developer:access permission not found');
    return;
  }

  // Find the Admin role
  const adminRole = await prisma.role.findFirst({
    where: { name: 'Admin' },
  });

  if (!adminRole) {
    console.log('Admin role not found');
    return;
  }

  // Check if Admin has developer:access
  const rolePermission = await prisma.rolePermission.findFirst({
    where: {
      roleId: adminRole.id,
      permissionId: developerPerm.id,
    },
  });

  if (!rolePermission) {
    console.log('Admin role does not have developer:access permission');
    return;
  }

  // Remove the permission
  await prisma.rolePermission.delete({
    where: {
      roleId_permissionId: {
        roleId: adminRole.id,
        permissionId: developerPerm.id,
      },
    },
  });

  console.log('Successfully removed developer:access permission from Admin role!');
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
