const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
  // 查找香港小学租户
  const tenant = await prisma.tenant.findFirst({
    where: { code: 'school001' }
  });

  if (!tenant) {
    console.log('租户 school001 不存在');
    return;
  }

  console.log(`租户: ${tenant.name} (${tenant.code})\n`);

  // 查找该租户的 student 角色
  const role = await prisma.role.findFirst({
    where: { tenantId: tenant.id, code: 'student' },
    include: {
      permissions: {
        include: {
          permission: true
        }
      }
    }
  });

  if (!role) {
    console.log('student 角色不存在');
    return;
  }

  console.log(`角色: ${role.name} (${role.code})`);
  console.log(`权限数量: ${role.permissions.length}\n`);

  console.log('学生角色所有权限:');
  role.permissions.forEach(rp => {
    console.log(`  ${rp.permission.code}`);
  });

  // 检查是否有 registration:read 权限
  const hasRegistrationRead = role.permissions.some(rp => rp.permission.code === 'registration:read');
  console.log(`\nregistration:read 权限: ${hasRegistrationRead ? '有' : '无'}`);
}

main()
  .then(() => prisma.$disconnect())
  .catch(e => {
    console.error(e);
    prisma.$disconnect();
  });
