import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  Post,
  Put,
  Query,
  UseGuards
} from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { JwtAuthGuard } from "../auth/guards/jwt-auth.guard";
import { IMeta } from "src/common/interfaces/IMeta";
import { CreateRoleDto } from "./dto/create-role.dto";
import { RoleQueryDto } from "./dto/role.query.dto";
import { Roles } from "../../share/decorators/roles.decorator";
import { RolesGuard } from "../../share/guards/roles.guard";
import { RolesService } from "./roles.service";
import { Role } from "src/database/models/role.model";
import { UpdateRoleDto } from "./dto/update-role.dto";
import { UpdateRolePermissionDto } from "./dto/update-role-permission.dto";
import { assignPermissionDto } from "./dto/assign-permission.dto";

@ApiTags("roles")
@ApiBearerAuth()
@Controller("roles")
export class RolesController {
  constructor(
    private readonly rolesService: RolesService  
  ) {}

  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Get()
  @Roles("role.view")
  @HttpCode(HttpStatus.OK)
  async findAll(
    @Query() query: RoleQueryDto
  ): Promise<{ meta: IMeta; data: Role[] }> {
    const data = await this.rolesService.findAll(query);
    const count = await this.rolesService.count(query);
    const { page, limit, orderBy, order } = query;
    return {
      meta: {
        totalData: count,
        totalPage: Math.ceil(count / query.limit),
        page,
        limit,
        orderBy,
        order,
      },
      data,
    };
  }

  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @Get(":id")
  @Roles("role.view")
  @HttpCode(HttpStatus.OK)
  find(@Param("id") id: number): Promise<Role> {
    return this.rolesService.findOne(+id);
  }
}
