import { Controller, Post, Get, Body, Param } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { RelayService } from './relay.service';
import { MetaTransactionDto } from './dto/meta-transaction.dto';
import { NonceManagerService } from '../nonce/nonce-manager.service';

@ApiTags('relay')
@Controller('v1/relay')
export class RelayController {
  constructor(
    private readonly relayService: RelayService,
    private readonly nonceManager: NonceManagerService,
  ) {}

  @Post()
  @ApiOperation({ summary: '提交 Meta-Transaction（用户签名 EIP-712，Relayer 代付 Gas）' })
  relay(@Body() metaTx: MetaTransactionDto) {
    return this.relayService.relay(metaTx);
  }

  @Get('nonce/:address')
  @ApiOperation({ summary: '获取用户当前 nonce（用于构造 meta-tx）' })
  getNonce(@Param('address') address: string) {
    return this.nonceManager.getUserRateCount(address).then((count) => ({
      address,
      recentRequests: count,
    }));
  }
}
