import { Controller } from '@nestjs/common';
import { AppService } from './app.service';
import { EventPattern } from '@nestjs/microservices';
import { RbacService } from './rbac/rbac.service';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly rbacService: RbacService,
  ) {}

  @EventPattern('cert.authorize')
  async authorize(payload) {
    console.log('获取user的token,进行payload权限的验证', payload);
    return false;
  }

  @EventPattern('cert.init.role.admin')
  async initRole() {
    await this.rbacService.initSuperAdminRole();
    await this.rbacService.initSuperAdminAccount();
  }

  @EventPattern('cert.create.role')
  async createRole(payload) {
    return await this.rbacService.createRole(payload);
  }

  @EventPattern('cert.remove.role')
  async removeRole(payload) {
    return await this.rbacService.removeRole(payload);
  }

  @EventPattern('cert.find.role.all')
  async findAllRole() {
    return await this.rbacService.findAllRole();
  }

  @EventPattern('cert.init.permission.resource')
  async initPermission(payload) {
    await this.rbacService.initPermission(payload);
  }

  @EventPattern('cert.find.role.permission')
  async findRolePermission(payload) {
    return await this.rbacService.findRolePermission(payload);
  }

  @EventPattern('cert.update.role.permissions')
  async updateRolePermissions(payload) {
    return await this.rbacService.updateRolePermissions(payload);
  }

  @EventPattern('cert.update.role')
  async updateRole(payload) {
    return await this.rbacService.updateRole(payload);
  }

  @EventPattern('cert.find.all.user')
  async findAllUser() {
    return await this.rbacService.findAllUser();
  }

  @EventPattern('cert.role.authorize')
  async roleAuthorize({ user, url }) {
    const allow = await this.rbacService.roleAuthorize(user, url);
    return { allow };
  }
}