import {
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  Post,
  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 {
  PalpitarDto,
  PalpitarResponse,
  RodadaResponse,
} from './dto/palpite.dto';
import { PredictionsService } from './predictions.service';

@ApiTags('Palpites')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('pools/:poolId')
export class PredictionsController {
  constructor(private readonly predictions: PredictionsService) {}

  /** Jogos da rodada, com prazo e meu palpite. Traz o horário do servidor. */
  @Get('matchdays/:matchday')
  @ApiOkResponse({ type: RodadaResponse })
  rodada(
    @Param('poolId') poolId: string,
    @Param('matchday', ParseIntPipe) matchday: number,
    @Req() req: RequestAutenticado,
  ): Promise<RodadaResponse> {
    return this.predictions.rodada(poolId, matchday, req.conta.id);
  }

  @Get('matchdays/:matchday/my-predictions')
  meusPalpites(
    @Param('poolId') poolId: string,
    @Param('matchday', ParseIntPipe) matchday: number,
    @Req() req: RequestAutenticado,
  ) {
    return this.predictions.meusPalpites(poolId, matchday, req.conta.id);
  }

  /**
   * Palpites de todos num jogo. Vazio com `revealed: false` enquanto o jogo
   * não terminar — inclusive para o admin.
   */
  @Get('matches/:matchId/predictions')
  palpitesDoJogo(
    @Param('poolId') poolId: string,
    @Param('matchId') matchId: string,
    @Req() req: RequestAutenticado,
  ) {
    return this.predictions.palpitesDoJogo(poolId, matchId, req.conta.id);
  }

  /** Envio em lote, com resultado item a item. */
  @Post('predictions')
  @ApiOkResponse({ type: PalpitarResponse })
  palpitar(
    @Param('poolId') poolId: string,
    @Body() dto: PalpitarDto,
    @Req() req: RequestAutenticado,
  ): Promise<PalpitarResponse> {
    return this.predictions.palpitar(poolId, req.conta.id, dto);
  }
}
