import {
  Body,
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  Post,
  Put,
  Patch,
  ParseIntPipe,
  Req,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Roles } from 'src/roles/roles.decorator';
import { RoleEnum } from 'src/roles/roles.enum';
import { RolesGuard } from 'src/roles/roles.guard';
import { CreateTeamDTO } from './dto/create-team.dto';
import { TeamsService } from './teams.service';
import { PublicTeamAccessService } from './public-team-access.service';
import { UpdatePublicAccessDto } from './dto/public-access.dto';

@ApiTags('Teams')
@Controller({
  path: 'teams',
  version: '1',
})
export class TeamsController {
  constructor(
    private service: TeamsService,
    private publicAccess: PublicTeamAccessService,
  ) {}

  @ApiBearerAuth()
  @UseGuards(AuthGuard('jwt'))
  @Get(':id/public-access')
  getPublicAccess(@Req() req, @Param('id', ParseIntPipe) id: number) {
    return this.publicAccess.getStatus(Number(req.user.id), id);
  }

  @ApiBearerAuth()
  @UseGuards(AuthGuard('jwt'))
  @Patch(':id/public-access')
  setPublicAccess(
    @Req() req,
    @Param('id', ParseIntPipe) id: number,
    @Body() body: UpdatePublicAccessDto,
  ) {
    return this.publicAccess.setEnabled(Number(req.user.id), id, body.enabled);
  }

  @ApiBearerAuth()
  @UseGuards(AuthGuard('jwt'))
  @Post(':id/public-access/rotate')
  rotatePublicAccess(@Req() req, @Param('id', ParseIntPipe) id: number) {
    return this.publicAccess.rotate(Number(req.user.id), id);
  }

  @ApiBearerAuth()
  @UseGuards(AuthGuard('jwt'))
  @Get(':id/players/:playerId/transactions')
  getPlayerTransactions(
    @Req() req,
    @Param('id', ParseIntPipe) id: number,
    @Param('playerId', ParseIntPipe) playerId: number,
  ) {
    return this.publicAccess.getMemberPlayerTransactions(
      Number(req.user.id),
      id,
      playerId,
    );
  }

  @ApiBearerAuth()
  @Roles([RoleEnum.user, RoleEnum.admin])
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Get(':id/overview')
  @HttpCode(HttpStatus.OK)
  findOne(@Param('id') id: string) {
    return this.service.getOverview(id);
  }

  @ApiOperation({
    summary: 'Transactionen für ein Team',
    description:
      'Gibt alle Transaktionen, Spieler und Teamwallet, für ein bestimmtes Team zurück und sortiert absteigend nach Datum',
  })
  @ApiBearerAuth()
  @Roles([RoleEnum.user, RoleEnum.admin])
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Get(':id/transactions')
  @HttpCode(HttpStatus.OK)
  getAllTransactions(@Req() req, @Param('id') id: string) {
    const userId = req.user?.id;
    return this.service.getTeamTransactions(id, userId);
  }

  @ApiOperation({
    summary: 'Neuen Spieler anlegen',
    description:
      'Erstellt einen neuen Spieler für das Team mit der ID aus der URL',
  })
  @ApiBearerAuth()
  @Roles([RoleEnum.user, RoleEnum.admin])
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Post(':id/players')
  @HttpCode(HttpStatus.CREATED)
  createPlayer(@Param('id') id: string, @Body() playerDto: any) {
    return this.service.createNewPlayer(id, playerDto);
  }

  @Roles([RoleEnum.user, RoleEnum.admin])
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @ApiOperation({
    summary: 'Spieler updaten',
    description: 'verändert den Spieler mit der team id',
  })
  @Put(':id/players')
  @HttpCode(HttpStatus.CREATED)
  updatePlayer(@Param('id') id: string, @Body() playerDto: any) {
    return this.service.updatePlayer(playerDto);
  }

  @ApiOperation({
    summary: 'Neues Team anlegen',
    description:
      'Erstellt ein neues Team mit gegebenem Namen und gibt es zurück.',
  })
  @ApiBearerAuth()
  @Roles([RoleEnum.admin])
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Post()
  @HttpCode(HttpStatus.CREATED)
  create(@Req() req, @Body() teamDto: CreateTeamDTO) {
    const userId = req.user?.id;
    return this.service.createNewTeam(teamDto, userId);
  }
}
