import { Controller, Get, Post, Body, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { UserWalletService } from '../../application/services/user-wallet.service';
import { CurrentUser, CurrentUserPayload } from '../../shared/decorators/current-user.decorator';
import { UserWalletType, AssetType, TransactionType } from '@prisma/client';
import Decimal from 'decimal.js';

class TransferDto {
  toAccountSeq: string;
  walletType: UserWalletType;
  assetType: AssetType;
  amount: string;
  memo?: string;
}

@ApiTags('User Wallets')
@Controller('user-wallets')
@ApiBearerAuth()
export class UserWalletController {
  constructor(private readonly userWalletService: UserWalletService) {}

  @Get('my')
  @ApiOperation({ summary: '获取当前用户所有钱包' })
  @ApiResponse({ status: 200, description: '用户钱包列表' })
  async getMyWallets(@CurrentUser() user: CurrentUserPayload) {
    return this.userWalletService.getUserWallets(user.accountSequence);
  }

  @Get('my/summary')
  @ApiOperation({ summary: '获取当前用户钱包汇总' })
  async getMyWalletSummary(@CurrentUser() user: CurrentUserPayload) {
    return this.userWalletService.getUserWalletSummary(user.accountSequence);
  }

  @Get('my/:walletType')
  @ApiOperation({ summary: '获取当前用户特定类型钱包' })
  async getMyWallet(
    @CurrentUser() user: CurrentUserPayload,
    @Param('walletType') walletType: UserWalletType,
  ) {
    return this.userWalletService.getUserWallet(user.accountSequence, walletType);
  }

  @Get('my/transactions')
  @ApiOperation({ summary: '获取当前用户交易记录' })
  @ApiQuery({ name: 'walletType', required: false })
  @ApiQuery({ name: 'transactionType', required: false })
  @ApiQuery({ name: 'assetType', required: false })
  @ApiQuery({ name: 'startDate', required: false })
  @ApiQuery({ name: 'endDate', required: false })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'offset', required: false, type: Number })
  async getMyTransactions(
    @CurrentUser() user: CurrentUserPayload,
    @Query('walletType') walletType?: UserWalletType,
    @Query('transactionType') transactionType?: TransactionType,
    @Query('assetType') assetType?: AssetType,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
    @Query('limit') limit?: number,
    @Query('offset') offset?: number,
  ) {
    return this.userWalletService.getTransactions(user.accountSequence, {
      walletType,
      transactionType,
      assetType,
      startDate: startDate ? new Date(startDate) : undefined,
      endDate: endDate ? new Date(endDate) : undefined,
      limit: limit ? Number(limit) : undefined,
      offset: offset ? Number(offset) : undefined,
    });
  }

  @Post('my/transfer')
  @ApiOperation({ summary: '用户间转账' })
  async transfer(
    @CurrentUser() user: CurrentUserPayload,
    @Body() dto: TransferDto,
  ) {
    return this.userWalletService.transferBetweenUsers(
      user.accountSequence,
      dto.toAccountSeq,
      dto.walletType,
      dto.assetType,
      new Decimal(dto.amount),
      { memo: dto.memo },
    );
  }

  @Post('create')
  @ApiOperation({ summary: '为用户创建所有钱包' })
  async createWallets(@CurrentUser() user: CurrentUserPayload) {
    return this.userWalletService.createWalletsForUser(user.accountSequence);
  }
}
