import {
  Controller,
  Get,
  Post,
  Body,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { ActivityService } from './activity.service';
import { JwtAuthGuard, CurrentUser } from '@/common';
import {
  BindInvitationDto,
  CreateInviteWithdrawalDto,
  QueryInviteRecordsDto,
} from './dto/activity.dto';

// ===== 用户端活动接口 =====
@ApiTags('用户端-活动')
@Controller('app/activity')
export class ActivityController {
  constructor(private readonly activityService: ActivityService) {}

  @Get('list')
  @ApiOperation({ summary: '获取活动列表' })
  async getActivityList() {
    const activity = await this.activityService.getEnabledInviteCashbackActivity();
    return activity ? [activity] : [];
  }

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

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('invite/stats')
  @ApiOperation({ summary: '获取我的邀请统计' })
  async getStats(@CurrentUser('sub') userId: number) {
    return this.activityService.getUserStats(userId);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Post('invite/bind')
  @ApiOperation({ summary: '绑定邀请关系' })
  async bind(@CurrentUser('sub') userId: number, @Body() dto: BindInvitationDto) {
    return this.activityService.bindInvitation(userId, dto);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('invite/records')
  @ApiOperation({ summary: '邀请记录列表' })
  async getRecords(@CurrentUser('sub') userId: number, @Query() dto: QueryInviteRecordsDto) {
    return this.activityService.getInviteRecords(userId, dto);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('invite/cashbacks')
  @ApiOperation({ summary: '返现记录列表' })
  async getCashbacks(@CurrentUser('sub') userId: number, @Query() dto: QueryInviteRecordsDto) {
    return this.activityService.getCashbackRecords(userId, dto);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Post('invite/withdraw')
  @ApiOperation({ summary: '申请提现' })
  async createWithdrawal(@CurrentUser('sub') userId: number, @Body() dto: CreateInviteWithdrawalDto) {
    return this.activityService.createWithdrawal(userId, dto);
  }

  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Get('invite/withdrawals')
  @ApiOperation({ summary: '提现记录列表' })
  async getWithdrawals(@CurrentUser('sub') userId: number, @Query() dto: QueryInviteRecordsDto) {
    return this.activityService.getWithdrawalRecords(userId, dto);
  }
}
