import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { MemberService } from '../../services/admin/MemberService';
import { CreateMemberDto, UpdateMemberDto, QueryMemberDto, BatchUpdateStatusDto, BatchAssignLevelDto, AdjustPointsDto, AdjustBalanceDto, ResetPasswordDto } from '../../dto/admin/MemberDto';
import { Roles } from '../../../auth/decorators/RolesDecorator';
import { JwtAuthGuard } from '../../../auth/guards/JwtAuthGuard';
import { RolesGuard } from '../../../auth/guards/RolesGuard';

@ApiTags('后台-会员管理')
@Controller('adminapi/member')
@UseGuards(JwtAuthGuard, RolesGuard)
@ApiBearerAuth()
export class MemberController {
  constructor(private readonly memberService: MemberService) {}

  @Post()
  @Roles('admin')
  @ApiOperation({ summary: '创建会员' })
  @ApiResponse({ status: 201, description: '会员创建成功' })
  async createMember(@Body() createMemberDto: CreateMemberDto) {
    return await this.memberService.createMember(createMemberDto);
  }

  @Get()
  @ApiOperation({ summary: '获取会员列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getMemberList(@Query() queryMemberDto: QueryMemberDto) {
    return await this.memberService.getMemberList(queryMemberDto);
  }

  @Get(':id')
  @Roles('admin')
  @ApiOperation({ summary: '获取会员详情' })
  @ApiResponse({ status: 200, description: '获取会员详情成功' })
  async getMemberDetail(@Param('id') id: number) {
    return await this.memberService.getMemberDetail(id);
  }

  @Put(':id')
  @Roles('admin')
  @ApiOperation({ summary: '更新会员' })
  @ApiResponse({ status: 200, description: '会员更新成功' })
  async updateMember(
    @Param('id') id: number,
    @Body() updateMemberDto: UpdateMemberDto
  ) {
    return await this.memberService.updateMember(id, updateMemberDto);
  }

  @Delete(':id')
  @Roles('admin')
  @ApiOperation({ summary: '删除会员' })
  @ApiResponse({ status: 200, description: '会员删除成功' })
  async deleteMember(@Param('id') id: number) {
    await this.memberService.deleteMember(id);
    return { message: '删除成功' };
  }

  @Post('batch-delete')
  @Roles('admin')
  @ApiOperation({ summary: '批量删除会员' })
  @ApiResponse({ status: 200, description: '批量删除成功' })
  async batchDeleteMembers(@Body() body: { member_ids: number[] }) {
    await this.memberService.batchDeleteMembers(body.member_ids);
    return { message: '批量删除成功' };
  }

  @Post('batch-update-status')
  @ApiOperation({ summary: '批量更新会员状态' })
  @ApiResponse({ status: 200, description: '状态更新成功' })
  async batchUpdateMemberStatus(@Body() batchUpdateStatusDto: BatchUpdateStatusDto) {
    await this.memberService.batchUpdateMemberStatus(batchUpdateStatusDto.member_ids, batchUpdateStatusDto.status);
    return { message: '状态更新成功' };
  }

  @Post('batch-assign-level')
  @Roles('admin')
  @ApiOperation({ summary: '批量分配会员等级' })
  @ApiResponse({ status: 200, description: '批量分配等级成功' })
  async batchAssignMemberLevel(@Body() batchAssignLevelDto: BatchAssignLevelDto) {
    await this.memberService.batchAssignMemberLevel(batchAssignLevelDto.member_ids, batchAssignLevelDto.level_id);
    return { message: '批量分配等级成功' };
  }

  @Post('adjust-points')
  @ApiOperation({ summary: '调整会员积分' })
  @ApiResponse({ status: 200, description: '积分调整成功' })
  async adjustMemberPoints(@Body() adjustPointsDto: AdjustPointsDto) {
    await this.memberService.adjustMemberPoints(adjustPointsDto.member_id, adjustPointsDto.points, adjustPointsDto.reason);
    return { message: '积分调整成功' };
  }

  @Post('adjust-balance')
  @ApiOperation({ summary: '调整会员余额' })
  @ApiResponse({ status: 200, description: '余额调整成功' })
  async adjustMemberBalance(@Body() adjustBalanceDto: AdjustBalanceDto) {
    await this.memberService.adjustMemberBalance(adjustBalanceDto.member_id, adjustBalanceDto.amount, adjustBalanceDto.reason);
    return { message: '余额调整成功' };
  }

  @Post(':id/reset-password')
  @ApiOperation({ summary: '重置会员密码' })
  @ApiResponse({ status: 200, description: '密码重置成功' })
  async resetMemberPassword(@Param('id') id: number, @Body() resetPasswordDto: ResetPasswordDto) {
    await this.memberService.resetMemberPassword(id, resetPasswordDto.new_password);
    return { message: '密码重置成功' };
  }

  @Put(':id/status')
  @ApiOperation({ summary: '更新会员状态' })
  @ApiResponse({ status: 200, description: '状态更新成功' })
  async updateMemberStatus(@Param('id') id: number, @Body() body: { status: number }) {
    await this.memberService.updateMemberStatus(id, body.status);
    return { message: '状态更新成功' };
  }

  @Get('export/list')
  @ApiOperation({ summary: '导出会员列表' })
  @ApiResponse({ status: 200, description: '导出成功' })
  async exportMembers(@Query('site_id') site_id: number) {
    return await this.memberService.exportMembers(site_id);
  }

  @Get('stats/overview')
  @ApiOperation({ summary: '获取会员统计概览' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getMemberStats(@Query('site_id') site_id: number) {
    return await this.memberService.getMemberStats(site_id);
  }
} 