/**
 * Add admin permission for GM position.
 * Run this for existing databases where seed was run before admin module existed:
 * npx ts-node scripts/add-admin-permission.ts
 */
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  const gmPosition = await prisma.position.findFirst({ where: { code: 'GM' } });
  if (!gmPosition) {
    console.log('GM position not found. Run full seed first.');
    process.exit(1);
  }

  const existing = await prisma.positionPermission.findFirst({
    where: { positionId: gmPosition.id, module: 'admin' },
  });
  if (existing) {
    console.log('Admin permission already exists for GM.');
    process.exit(0);
  }

  await prisma.positionPermission.create({
    data: {
      positionId: gmPosition.id,
      module: 'admin',
      resource: '*',
      actions: ['*'],
    },
  });
  console.log('Admin permission added for GM position.');
}

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