import {
  Controller,
  DefaultValuePipe,
  Get,
  Param,
  ParseIntPipe,
  Query,
  Req,
  UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import type { RequestAutenticado } from '../auth/jwt-auth.guard';
import { ExtratoService } from './extrato.service';
import { RankingService } from './ranking.service';

@ApiTags('Ranking')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('pools/:poolId')
export class RankingController {
  constructor(
    private readonly ranking: RankingService,
    private readonly extrato: ExtratoService,
  ) {}

  @Get('ranking')
  @ApiOkResponse({ description: 'ranking geral, com desempate aplicado' })
  geral(
    @Param('poolId') poolId: string,
    @Req() req: RequestAutenticado,
    @Query('limit', new DefaultValuePipe(50), ParseIntPipe) limit: number,
    @Query('offset', new DefaultValuePipe(0), ParseIntPipe) offset: number,
  ) {
    return this.ranking.geral(poolId, req.conta.id, limit, offset);
  }

  @Get('matchdays/:matchday/ranking')
  @ApiOkResponse({
    description: 'ranking da rodada, com aviso de apuração parcial',
  })
  daRodada(
    @Param('poolId') poolId: string,
    @Param('matchday', ParseIntPipe) matchday: number,
    @Req() req: RequestAutenticado,
    @Query('limit', new DefaultValuePipe(50), ParseIntPipe) limit: number,
    @Query('offset', new DefaultValuePipe(0), ParseIntPipe) offset: number,
  ) {
    return this.ranking.daRodada(poolId, matchday, req.conta.id, limit, offset);
  }

  @Get('matchdays/:matchday/statement')
  @ApiOkResponse({
    description: 'extrato do participante, com a conta por jogo',
  })
  extratoDaRodada(
    @Param('poolId') poolId: string,
    @Param('matchday', ParseIntPipe) matchday: number,
    @Req() req: RequestAutenticado,
  ) {
    return this.extrato.doParticipante(poolId, matchday, req.conta.id);
  }
}
