import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Put,
  Req,
  UseGuards,
} from '@nestjs/common';
import { UserRole } from '@prisma/client';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import type { AuthenticatedRequest } from '../auth/jwt-auth.guard';
import { Roles } from '../auth/roles.decorator';
import { RolesGuard } from '../auth/roles.guard';
import { CreateCardDto } from './dto/create-card.dto';
import {
  SetOutcomesDto,
  UpsertPredictionEntryDto,
  ValidateScoresDto,
} from './dto/prediction-value.dto';
import { UpdateCardDto } from './dto/update-card.dto';
import { PredictionsService } from './predictions.service';

@Controller('predictions')
@UseGuards(JwtAuthGuard)
export class PredictionsController {
  constructor(private readonly predictionsService: PredictionsService) {}

  @Get('cards')
  listCards() {
    return this.predictionsService.listCards(false);
  }

  @Get('board')
  board(
    @Req() request: AuthenticatedRequest,
    @Query('selectedBabyIndex') selectedBabyIndex?: string,
  ) {
    return this.predictionsService.getBoard(
      request.user!.sub,
      undefined,
      selectedBabyIndex ? Number(selectedBabyIndex) : undefined,
    );
  }

  @Get('activity')
  activity(@Req() request: AuthenticatedRequest) {
    return this.predictionsService.listActivity(request.user!.sub);
  }

  @Get('scoreboard')
  scoreboard() {
    return this.predictionsService.getScoreboard();
  }

  @Get('my-entries')
  myEntries(@Req() request: AuthenticatedRequest) {
    return this.predictionsService.listMyEntries(request.user!.sub);
  }

  @Put('cards/:cardId/my-entry')
  upsertMyEntry(
    @Req() request: AuthenticatedRequest,
    @Param('cardId') cardId: string,
    @Body() dto: UpsertPredictionEntryDto,
  ) {
    return this.predictionsService.upsertMyPrediction(request.user!.sub, cardId, dto);
  }

  @Post('game/close')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  closeGame(@Req() request: AuthenticatedRequest) {
    return this.predictionsService.closeGame(request.user!.sub);
  }

  @Post('game/open')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  openGame(@Req() request: AuthenticatedRequest) {
    return this.predictionsService.openGame(request.user!.sub);
  }

  @Post('game/finalize')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  finalizeGame(@Req() request: AuthenticatedRequest) {
    return this.predictionsService.finalizeGame(request.user!.sub);
  }

  @Post('cards')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  createCard(@Req() request: AuthenticatedRequest, @Body() dto: CreateCardDto) {
    return this.predictionsService.createCard(request.user!.sub, dto);
  }

  @Patch('cards/:cardId')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  updateCard(
    @Req() request: AuthenticatedRequest,
    @Param('cardId') cardId: string,
    @Body() dto: UpdateCardDto,
  ) {
    return this.predictionsService.updateCard(request.user!.sub, cardId, dto);
  }

  @Delete('cards/:cardId')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  deleteCard(@Req() request: AuthenticatedRequest, @Param('cardId') cardId: string) {
    return this.predictionsService.deleteCard(request.user!.sub, cardId);
  }

  @Post('cards/:cardId/outcomes')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  setOutcomes(
    @Req() request: AuthenticatedRequest,
    @Param('cardId') cardId: string,
    @Body() dto: SetOutcomesDto,
  ) {
    return this.predictionsService.setCardOutcomes(request.user!.sub, cardId, dto);
  }

  @Post('cards/:cardId/suggest-scores')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  suggestScores(
    @Req() request: AuthenticatedRequest,
    @Param('cardId') cardId: string,
    @Query('selectedBabyIndex') selectedBabyIndex?: string,
  ) {
    return this.predictionsService.suggestScoresForCard(
      request.user!.sub,
      cardId,
      undefined,
      selectedBabyIndex ? Number(selectedBabyIndex) : undefined,
    );
  }

  @Patch('entries/:entryId/scores')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  validateScores(
    @Req() request: AuthenticatedRequest,
    @Param('entryId') entryId: string,
    @Body() dto: ValidateScoresDto,
  ) {
    return this.predictionsService.validateScores(request.user!.sub, entryId, dto);
  }
}
