import {
  Controller,
  Post,
  Get,
  Put,
  Body,
  Param,
  Query,
  UseGuards,
  Req,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AdminAuthService } from './auth.service';
import {
  AdminLoginDto,
  CreateAdminDto,
  UpdateAdminDto,
  ChangeAdminPasswordDto,
} from './dto/auth.dto';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';
import { RolesGuard } from '@/common/guards/roles.guard';
import { Roles } from '@/common/decorators/roles.decorator';
import { CurrentUser } from '@/common/decorators/current-user.decorator';

@ApiTags('管理员认证')
@Controller('admin/auth')
export class AdminAuthController {
  constructor(private readonly adminAuthService: AdminAuthService) {}

  @Post('login')
  @ApiOperation({ summary: '管理员登录' })
  async login(@Body() dto: AdminLoginDto) {
    return this.adminAuthService.login(dto);
  }

  @Get('profile')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '获取管理员信息' })
  async getProfile(
    @CurrentUser('sub') id: number,
    @CurrentUser('type') type: string,
  ) {
    if (type !== 'admin') throw new Error('无权限');
    return this.adminAuthService.getProfile(id);
  }

  @Put('profile')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '更新管理员信息' })
  async updateProfile(
    @CurrentUser('sub') id: number,
    @CurrentUser('type') type: string,
    @Body() dto: UpdateAdminDto,
  ) {
    if (type !== 'admin') throw new Error('无权限');
    return this.adminAuthService.updateProfile(id, dto);
  }

  @Put('password')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '修改管理员密码' })
  async changePassword(
    @CurrentUser('sub') id: number,
    @CurrentUser('type') type: string,
    @Body() dto: ChangeAdminPasswordDto,
  ) {
    if (type !== 'admin') throw new Error('无权限');
    return this.adminAuthService.changePassword(id, dto);
  }

  @Post('create')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @ApiBearerAuth()
  @ApiOperation({ summary: '创建管理员(超级管理员)' })
  async createAdmin(
    @CurrentUser('adminRole') adminRole: string,
    @Body() dto: CreateAdminDto,
  ) {
    if (adminRole !== 'super_admin') throw new Error('仅超级管理员可创建');
    return this.adminAuthService.createAdmin(dto);
  }

  @Get('list')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @ApiBearerAuth()
  @ApiOperation({ summary: '管理员列表' })
  async findAll(
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.adminAuthService.findAll(page, pageSize);
  }

  @Put(':id/freeze')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @ApiBearerAuth()
  @ApiOperation({ summary: '冻结管理员' })
  async freeze(@Param('id') id: number) {
    return this.adminAuthService.freeze(id);
  }

  @Put(':id/unfreeze')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('admin')
  @ApiBearerAuth()
  @ApiOperation({ summary: '解冻管理员' })
  async unfreeze(@Param('id') id: number) {
    return this.adminAuthService.unfreeze(id);
  }
}
