import { Controller, Post, Get, Body, Query, UseGuards, Request } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../shared/guards/jwt-auth.guard';
import { RewardApplicationService } from '../../application/services/reward-application.service';
import { SettleRewardsDto } from '../dto/request/settle-rewards.dto';
import { SettlementHistoryQueryDto } from '../dto/request/settlement-history-query.dto';
import { SettlementResultDto } from '../dto/response/settlement-result.dto';
import { SettlementHistoryResponseDto } from '../dto/response/settlement-history.dto';

@ApiTags('Settlement')
@Controller('rewards')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class SettlementController {
  constructor(private readonly rewardService: RewardApplicationService) {}

  @Post('settle')
  @ApiOperation({ summary: '结算可结算收益' })
  @ApiResponse({ status: 200, description: '成功', type: SettlementResultDto })
  @ApiResponse({ status: 400, description: '结算失败' })
  async settle(
    @Request() req,
    @Body() dto: SettleRewardsDto,
  ): Promise<SettlementResultDto> {
    const accountSequence = req.user.accountSequence;

    return this.rewardService.settleRewards({
      accountSequence,
      settleCurrency: dto.settleCurrency,
    });
  }

  @Get('settlement-history')
  @ApiOperation({ summary: '获取结算历史记录' })
  @ApiResponse({ status: 200, description: '成功', type: SettlementHistoryResponseDto })
  async getSettlementHistory(
    @Request() req,
    @Query() query: SettlementHistoryQueryDto,
  ): Promise<SettlementHistoryResponseDto> {
    const accountSequence = req.user.accountSequence;

    return this.rewardService.getSettlementHistory(accountSequence, {
      page: query.page || 1,
      pageSize: query.pageSize || 20,
    });
  }
}
