import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Query,
  Param,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { ActivityService } from './activity.service';
import { JwtAuthGuard, RolesGuard, Roles, CurrentUser } from '@/common';
import {
  CreateActivityDto,
  UpdateActivityDto,
  QueryActivityDto,
  RejectInviteWithdrawalDto,
  QueryAdminInviteWithdrawalsDto,
  QueryAdminCashbacksDto,
  QueryInviteRecordsDto,
} from './dto/activity.dto';

@ApiTags('管理端-活动管理')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@Controller('admin/activity')
export class AdminActivityController {
  constructor(private readonly activityService: ActivityService) {}

  @Get('list')
  @ApiOperation({ summary: '活动列表' })
  async list(@Query() dto: QueryActivityDto) {
    return this.activityService.getActivities(dto);
  }

  @Get(':id')
  @ApiOperation({ summary: '活动详情' })
  @ApiParam({ name: 'id', description: '活动ID' })
  async getById(@Param('id', ParseIntPipe) id: number) {
    return this.activityService.getActivityById(id);
  }

  @Post('create')
  @ApiOperation({ summary: '创建活动' })
  async create(@Body() dto: CreateActivityDto) {
    return this.activityService.createActivity(dto);
  }

  @Put(':id/update')
  @ApiOperation({ summary: '编辑活动' })
  @ApiParam({ name: 'id', description: '活动ID' })
  async update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateActivityDto) {
    return this.activityService.updateActivity(id, dto);
  }

  @Put(':id/toggle')
  @ApiOperation({ summary: '启用/停用活动' })
  @ApiParam({ name: 'id', description: '活动ID' })
  async toggle(@Param('id', ParseIntPipe) id: number) {
    return this.activityService.toggleActivity(id);
  }

  @Get('invite/stats')
  @ApiOperation({ summary: '邀请数据总览' })
  async getStats() {
    return this.activityService.getAdminStats();
  }

  @Get('invite/config')
  @ApiOperation({ summary: '获取邀请活动配置' })
  async getInviteConfig() {
    return this.activityService.getInviteCashbackConfig();
  }

  @Put('invite/config')
  @ApiOperation({ summary: '更新邀请活动配置' })
  async updateInviteConfig(@Body() dto: UpdateActivityDto) {
    return this.activityService.updateInviteCashbackConfig(dto);
  }

  @Get('invite/records')
  @ApiOperation({ summary: '邀请记录列表' })
  async getRecords(@Query() dto: QueryInviteRecordsDto) {
    return this.activityService.getAdminInvitations(dto);
  }

  @Get('invite/cashbacks')
  @ApiOperation({ summary: '返现记录列表' })
  async getCashbacks(@Query() dto: QueryAdminCashbacksDto) {
    return this.activityService.getAdminCashbacks(dto);
  }

  @Get('invite/withdrawals')
  @ApiOperation({ summary: '提现审核列表' })
  async getWithdrawals(@Query() dto: QueryAdminInviteWithdrawalsDto) {
    return this.activityService.getAdminWithdrawals(dto);
  }

  @Put('invite/withdrawals/:id/approve')
  @ApiOperation({ summary: '审核通过' })
  @ApiParam({ name: 'id', description: '提现申请ID' })
  async approve(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser('sub') reviewerId: number,
  ) {
    return this.activityService.approveWithdrawal(id, reviewerId);
  }

  @Put('invite/withdrawals/:id/reject')
  @ApiOperation({ summary: '审核拒绝' })
  @ApiParam({ name: 'id', description: '提现申请ID' })
  async reject(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser('sub') reviewerId: number,
    @Body() dto: RejectInviteWithdrawalDto,
  ) {
    return this.activityService.rejectWithdrawal(id, reviewerId, dto);
  }

  @Put('invite/withdrawals/:id/pay')
  @ApiOperation({ summary: '确认打款' })
  @ApiParam({ name: 'id', description: '提现申请ID' })
  async pay(@Param('id', ParseIntPipe) id: number) {
    return this.activityService.payWithdrawal(id);
  }
}
