import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Request,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth } from '@nestjs/swagger';
import { CreatePenaltyDTO } from './dto/create-penalty.dto';
import { UpdatePenaltyDTO } from './dto/update-penalty.dto';
import { PenaltyService } from './penalty.service';

type AuthenticatedRequest = { user: { id: number } };

@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@Controller({ path: 'penalty', version: '1' })
export class PenaltyController {
  constructor(private readonly service: PenaltyService) {}

  @Get()
  getUserPenalties(@Request() request: AuthenticatedRequest) {
    return this.service.getUserPenalties(request.user.id);
  }

  @Get(':teamId')
  getTeamPenalties(
    @Request() request: AuthenticatedRequest,
    @Param('teamId', ParseIntPipe) teamId: number,
  ) {
    return this.service.getTeamPenalties(request.user.id, teamId);
  }

  @Post()
  createPenalty(
    @Request() request: AuthenticatedRequest,
    @Body() dto: CreatePenaltyDTO,
  ) {
    return this.service.createPenalty(dto, request.user.id);
  }

  @Patch(':penaltyId')
  updatePenalty(
    @Request() request: AuthenticatedRequest,
    @Param('penaltyId', ParseIntPipe) penaltyId: number,
    @Body() dto: UpdatePenaltyDTO,
  ) {
    return this.service.updatePenalty(penaltyId, dto, request.user.id);
  }

  @Delete(':penaltyId')
  @HttpCode(HttpStatus.NO_CONTENT)
  async deletePenalty(
    @Request() request: AuthenticatedRequest,
    @Param('penaltyId', ParseIntPipe) penaltyId: number,
  ): Promise<void> {
    await this.service.deletePenalty(penaltyId, request.user.id);
  }
}
