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`);

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

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

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

  // 检查系统管理相关权限
  const systemPermissions = ['user:read', 'role:read', 'menu:read', 'permission:read'];
  console.log('系统管理相关权限:');
  systemPermissions.forEach(code => {
    const has = role.permissions.some(rp => rp.permission.code === code);
    console.log(`  ${code}: ${has ? '✓' : '✗'}`);
  });

  // 查找该租户的权限
  console.log('\n该租户所有权限:');
  const permissions = await prisma.permission.findMany({
    where: { tenantId: tenant.id }
  });
  permissions.forEach(p => {
    console.log(`  ${p.code}`);
  });
}

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