import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

import type { UsageListResponse, WalletResponse } from '@hexahost/contracts';

import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { SessionGuard } from '../auth/guards/session.guard';

import { BillingService } from './billing.service';

@ApiTags('account')
@Controller('account')
@UseGuards(SessionGuard)
export class BillingController {
  constructor(private readonly billingService: BillingService) {}

  @Get('wallet')
  @ApiOperation({ summary: 'Get credit wallet balance and recent transactions' })
  getWallet(@CurrentUser() user: { id: string }): Promise<WalletResponse> {
    return this.billingService.getWallet(user.id);
  }

  @Get('usage')
  @ApiOperation({ summary: 'List recent usage records' })
  listUsage(
    @CurrentUser() user: { id: string },
    @Query('limit') limit?: string,
  ): Promise<UsageListResponse> {
    const parsed = limit ? Number.parseInt(limit, 10) : 20;
    return this.billingService.listUsage(user.id, Number.isFinite(parsed) ? parsed : 20);
  }
}
