import { 
  Controller, 
  Get, 
  Post, 
  Put, 
  Delete, 
  Body, 
  Param, 
  Query, 
  UseGuards,
  HttpCode,
  HttpStatus
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { RoleAdminService } from '../../services/admin/RoleAdminService';
import { CreateRoleDto, UpdateRoleDto, QueryRoleDto, BatchUpdateStatusDto, AssignMenusDto } from '../../dto/admin/RoleDto';
import { JwtAuthGuard } from '../../../auth/guards/JwtAuthGuard';
import { RolesGuard } from '../../../auth/guards/RolesGuard';
import { Roles } from '../../../auth/decorators/RolesDecorator';

@ApiTags('角色管理')
@Controller('adminapi/role')
@UseGuards(JwtAuthGuard, RolesGuard)
@ApiBearerAuth()
export class RoleController {
  constructor(private readonly roleAdminService: RoleAdminService) {}

  @Post()
  @ApiOperation({ summary: '创建角色' })
  @ApiResponse({ status: 201, description: '角色创建成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @Roles('admin')
  async createRole(@Body() createRoleDto: CreateRoleDto) {
    return await this.roleAdminService.createRole(createRoleDto);
  }

  @Get()
  @ApiOperation({ summary: '获取角色列表' })
  @ApiResponse({ status: 200, description: '获取角色列表成功' })
  @Roles('admin')
  async getRoleList(@Query() queryRoleDto: QueryRoleDto) {
    return await this.roleAdminService.getRoleList(queryRoleDto);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取角色详情' })
  @ApiResponse({ status: 200, description: '获取角色详情成功' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @Roles('admin')
  async getRoleDetail(@Param('id') id: string) {
    return await this.roleAdminService.getRoleDetail(Number(id));
  }

  @Put(':id')
  @ApiOperation({ summary: '更新角色' })
  @ApiResponse({ status: 200, description: '角色更新成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @Roles('admin')
  async updateRole(
    @Param('id') id: string,
    @Body() updateRoleDto: UpdateRoleDto
  ) {
    return await this.roleAdminService.updateRole(Number(id), updateRoleDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除角色' })
  @ApiResponse({ status: 200, description: '角色删除成功' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @HttpCode(HttpStatus.OK)
  @Roles('admin')
  async deleteRole(@Param('id') id: string) {
    return await this.roleAdminService.deleteRole(Number(id));
  }

  @Delete('batch')
  @ApiOperation({ summary: '批量删除角色' })
  @ApiResponse({ status: 200, description: '批量删除角色成功' })
  @HttpCode(HttpStatus.OK)
  @Roles('admin')
  async batchDeleteRoles(@Body() body: { roleIds: number[] }) {
    return await this.roleAdminService.batchDeleteRoles(body.roleIds);
  }

  @Put(':id/status')
  @ApiOperation({ summary: '更新角色状态' })
  @ApiResponse({ status: 200, description: '角色状态更新成功' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @Roles('admin')
  async updateRoleStatus(
    @Param('id') id: string,
    @Body() body: { status: number }
  ) {
    return await this.roleAdminService.updateRoleStatus(Number(id), body.status);
  }

  @Put('batch/status')
  @ApiOperation({ summary: '批量更新角色状态' })
  @ApiResponse({ status: 200, description: '批量更新角色状态成功' })
  @Roles('admin')
  async batchUpdateRoleStatus(@Body() body: BatchUpdateStatusDto) {
    return await this.roleAdminService.batchUpdateRoleStatus(body.roleIds, body.status);
  }

  @Put(':id/menus')
  @ApiOperation({ summary: '分配菜单权限' })
  @ApiResponse({ status: 200, description: '菜单权限分配成功' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @Roles('admin')
  async assignMenus(
    @Param('id') id: string,
    @Body() assignMenusDto: AssignMenusDto
  ) {
    return await this.roleAdminService.assignMenus(Number(id), assignMenusDto.menuIds);
  }

  @Get('stats/overview')
  @ApiOperation({ summary: '获取角色统计信息' })
  @ApiResponse({ status: 200, description: '获取角色统计信息成功' })
  @Roles('admin')
  async getRoleStats() {
    return await this.roleAdminService.getRoleStats();
  }

  @Post('export')
  @ApiOperation({ summary: '导出角色数据' })
  @ApiResponse({ status: 200, description: '导出角色数据成功' })
  @Roles('admin')
  async exportRoles(@Body() query: any) {
    return await this.roleAdminService.exportRoles();
  }
} 