import { Controller, Get, UseGuards } from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiResponse,
} from '@nestjs/swagger';
import { MeteringService, UsageRecord, LimitCheck } from './metering.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles, UserRole } from '../../common/decorators/roles.decorator';
import {
  CurrentUser,
  JwtPayload,
} from '../../common/decorators/current-user.decorator';

@ApiTags('Metering')
@ApiBearerAuth()
@Controller('metering')
@UseGuards(JwtAuthGuard, RolesGuard)
export class MeteringController {
  constructor(private readonly meteringService: MeteringService) {}

  @Get('usage')
  @Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Get current month usage for the club' })
  @ApiResponse({ status: 200, description: 'Current usage metrics' })
  @ApiResponse({ status: 403, description: 'Insufficient role' })
  async getUsage(@CurrentUser() user: JwtPayload): Promise<UsageRecord> {
    // Aggregate fresh usage data
    return this.meteringService.aggregateUsage(user.clubId);
  }

  @Get('limits')
  @Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Check resource limits for the club' })
  @ApiResponse({ status: 200, description: 'Limit check results' })
  @ApiResponse({ status: 403, description: 'Insufficient role' })
  async checkLimits(@CurrentUser() user: JwtPayload): Promise<LimitCheck[]> {
    return this.meteringService.checkLimits(user.clubId);
  }
}
