import { Body, Controller, Post, Req } from '@nestjs/common';
import { AddTicketsDto } from './dtos/add-tickets.dto';
import { TicketsService } from '../../common/tickets.service';
import { v4 as uuidv4 } from 'uuid';
import { Roles } from 'api/utils/decorator';
import { RoleEnum } from 'api/utils/constants';
import { ExchangeWalletDto } from './dtos/exchange-wallet.dto';
import { Request } from 'express';
import { getUid } from 'api/utils/utils';

@Controller('tickets')
export class TicketsController {
  constructor(private ticketsService: TicketsService) {}
  @Post('add')
  @Roles(RoleEnum.SuperAdmin)
  async add(@Body() addTicketsDto: AddTicketsDto) {
    const { amount, num } = addTicketsDto;
    let textStr = '';
    for (let i = 0; i < num; i++) {
      const password = uuidv4().replaceAll('-', '').toUpperCase();
      this.ticketsService.save({
        amount: amount * 100,
        password,
      });
      textStr += `${password}\n`;
    }
    return textStr;
  }

  @Post('exchange')
  async exchange(
    @Req() req: Request,
    @Body() exchangeWalletDto: ExchangeWalletDto,
  ) {
    const uid = getUid(req);
    const { password } = exchangeWalletDto;
    await this.ticketsService.recharge(uid, password);
    return true;
  }
}
