import { Controller, Get, Post, Body, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { WalletService } from '../../../application/services/wallet.service';
import { DepositDto, WithdrawDto } from '../dto/wallet.dto';

@ApiTags('Wallet')
@Controller('wallet')
export class WalletController {
  constructor(private readonly walletService: WalletService) {}

  @Get()
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get wallet balance' })
  async getWallet(@Req() req: any) {
    const wallet = await this.walletService.getWallet(req.user.id);
    return { code: 0, data: wallet };
  }

  @Post('deposit')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Deposit funds to wallet' })
  async deposit(@Req() req: any, @Body() dto: DepositDto) {
    const result = await this.walletService.deposit(req.user.id, dto.amount, dto.description);
    return { code: 0, data: result };
  }

  @Post('withdraw')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Withdraw funds from wallet' })
  async withdraw(@Req() req: any, @Body() dto: WithdrawDto) {
    const result = await this.walletService.withdraw(req.user.id, dto.amount, dto.description);
    return { code: 0, data: result };
  }

  @Get('transactions')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get transaction history' })
  async getTransactions(
    @Req() req: any,
    @Query('page') page: string = '1',
    @Query('limit') limit: string = '20',
  ) {
    const result = await this.walletService.getTransactions(req.user.id, parseInt(page), parseInt(limit));
    return { code: 0, data: result };
  }
}
