import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { AdminService } from '../../services/admin/AdminService';
import { CreateAdminDto, UpdateAdminDto, QueryAdminDto, BatchUpdateStatusDto, BatchAssignRoleDto, ResetPasswordDto } from '../../dto/admin/AdminDto';
import { JwtAuthGuard } from '../../../auth/guards/JwtAuthGuard';
import { RolesGuard } from '../../../auth/guards/RolesGuard';
import { Roles } from '../../../auth/decorators/RolesDecorator';

@ApiTags('后台-管理员管理')
@Controller('adminapi/admin')
@UseGuards(JwtAuthGuard, RolesGuard)
@ApiBearerAuth()
export class AdminController {
  constructor(private readonly adminService: AdminService) {}

  @Post()
  @Roles('admin')
  @UsePipes(new ValidationPipe())
  @ApiOperation({ summary: '创建管理员' })
  @ApiResponse({ status: 201, description: '管理员创建成功' })
  async createAdmin(@Body() createAdminDto: CreateAdminDto) {
    return await this.adminService.createAdmin(createAdminDto);
  }

  @Get()
  @Roles('admin')
  @ApiOperation({ summary: '获取管理员列表' })
  @ApiResponse({ status: 200, description: '获取管理员列表成功' })
  async getAdminList(@Query() query: QueryAdminDto, @Query('site_id') site_id: number = 0) {
    return await this.adminService.getAdminList(query, site_id);
  }

  @Get(':id')
  @Roles('admin')
  @ApiOperation({ summary: '获取管理员详情' })
  @ApiResponse({ status: 200, description: '获取管理员详情成功' })
  async getAdminDetail(@Param('id') id: number, @Query('site_id') site_id: number = 0) {
    return await this.adminService.getAdminDetail(id, site_id);
  }

  @Put(':id')
  @Roles('admin')
  @ApiOperation({ summary: '更新管理员' })
  @ApiResponse({ status: 200, description: '管理员更新成功' })
  async updateAdmin(
    @Param('id') id: number,
    @Body() updateAdminDto: UpdateAdminDto,
    @Query('site_id') site_id: number = 0
  ) {
    return await this.adminService.updateAdmin(id, updateAdminDto, site_id);
  }

  @Delete(':id')
  @Roles('admin')
  @ApiOperation({ summary: '删除管理员' })
  @ApiResponse({ status: 200, description: '管理员删除成功' })
  async deleteAdmin(@Param('id') id: number, @Query('site_id') site_id: number = 0) {
    await this.adminService.deleteAdmin(id, site_id);
    return { message: '删除成功' };
  }

  @Post('batch-delete')
  @Roles('admin')
  @ApiOperation({ summary: '批量删除管理员' })
  @ApiResponse({ status: 200, description: '批量删除成功' })
  async batchDeleteAdmins(@Body() data: { uids: number[] }, @Query('site_id') site_id: number = 0) {
    await this.adminService.batchDeleteAdmins(data.uids, site_id);
    return { message: '批量删除成功' };
  }

  @Post('batch-update-status')
  @Roles('admin')
  @ApiOperation({ summary: '批量更新管理员状态' })
  @ApiResponse({ status: 200, description: '批量更新状态成功' })
  async batchUpdateAdminStatus(@Body() data: BatchUpdateStatusDto, @Query('site_id') site_id: number = 0) {
    await this.adminService.batchUpdateAdminStatus(data.uids, data.status, site_id);
    return { message: '批量更新状态成功' };
  }

  @Post('batch-assign-role')
  @Roles('admin')
  @ApiOperation({ summary: '批量分配角色' })
  @ApiResponse({ status: 200, description: '批量分配角色成功' })
  async batchAssignAdminRoles(@Body() data: BatchAssignRoleDto, @Query('site_id') site_id: number = 0) {
    await this.adminService.batchAssignAdminRoles(data.uids, data.role_ids, site_id);
    return { message: '批量分配角色成功' };
  }

  @Post(':id/reset-password')
  @Roles('admin')
  @ApiOperation({ summary: '重置管理员密码' })
  @ApiResponse({ status: 200, description: '密码重置成功' })
  async resetAdminPassword(
    @Param('id') id: number,
    @Body() resetPasswordDto: ResetPasswordDto,
    @Query('site_id') site_id: number = 0
  ) {
    await this.adminService.resetAdminPassword(id, resetPasswordDto, site_id);
    return { message: '密码重置成功' };
  }

  @Put(':id/status')
  @Roles('admin')
  @ApiOperation({ summary: '更新管理员状态' })
  @ApiResponse({ status: 200, description: '状态更新成功' })
  async updateAdminStatus(
    @Param('id') id: number,
    @Body() data: { status: number },
    @Query('site_id') site_id: number = 0
  ) {
    await this.adminService.updateAdminStatus(id, data.status, site_id);
    return { message: '状态更新成功' };
  }

  @Post(':id/assign-role')
  @Roles('admin')
  @ApiOperation({ summary: '分配角色' })
  @ApiResponse({ status: 200, description: '角色分配成功' })
  async assignAdminRoles(
    @Param('id') id: number,
    @Body() data: { role_ids: string },
    @Query('site_id') site_id: number = 0
  ) {
    await this.adminService.assignAdminRoles(id, data.role_ids, site_id);
    return { message: '角色分配成功' };
  }

  @Get('export/list')
  @Roles('admin')
  @ApiOperation({ summary: '导出管理员列表' })
  @ApiResponse({ status: 200, description: '导出成功' })
  async exportAdmins(@Query('site_id') site_id: number = 0) {
    return await this.adminService.exportAdmins(site_id);
  }

  @Get('stats/overview')
  @Roles('admin')
  @ApiOperation({ summary: '获取管理员统计信息' })
  @ApiResponse({ status: 200, description: '获取统计信息成功' })
  async getAdminStats(@Query('site_id') site_id: number = 0) {
    return await this.adminService.getAdminStats(site_id);
  }
} 