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

const prisma = new PrismaClient();

function getDefaultPermissions(role: string): string[] {
    const common = ['dashboard', 'opportunities', 'clients', 'activities', 'products'];
    switch (role) {
      case 'ADMIN':
      case 'GENERAL_MANAGER':
        return [...common, 'tracking', 'targets', 'incentives', 'reports', 'funnel-analysis', 'users', 'settings', 'expenses', 'call-logs'];
      case 'MANAGER':
        return [...common, 'reports', 'incentives', 'targets', 'call-logs'];
      default:
        return common;
    }
}

async function main() {
  const users = await prisma.user.findMany();
  for (const user of users) {
    if (!user.permissions) {
        const permissions = getDefaultPermissions(user.role);
        await prisma.user.update({
            where: { id: user.id },
            data: { permissions: JSON.stringify(permissions) }
        });
        console.log(`Updated permissions for ${user.email} (${user.role})`);
    }
  }
}

main()
  .catch(console.error)
  .finally(() => prisma.$disconnect());
