import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Request,
  ParseIntPipe,
  BadRequestException,
} from '@nestjs/common';
import { TeamsService } from './teams.service';
import { CreateTeamDto } from './dto/create-team.dto';
import { UpdateTeamDto } from './dto/update-team.dto';
import { InviteMemberDto } from './dto/invite-member.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests/teams')
@UseGuards(JwtAuthGuard)
export class TeamsController {
  constructor(private readonly teamsService: TeamsService) {}

  @Post()
  @RequirePermission('team:create')
  create(@Body() createTeamDto: CreateTeamDto, @Request() req) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new BadRequestException('无法确定租户信息');
    }
    const creatorId = req.user?.userId;
    return this.teamsService.create(createTeamDto, tenantId, creatorId);
  }

  @Get('contest/:contestId')
  @RequirePermission('team:read')
  findAll(
    @Param('contestId', ParseIntPipe) contestId: number,
    @Request() req,
  ) {
    const tenantId = req.tenantId || req.user?.tenantId;
    return this.teamsService.findAll(contestId, tenantId);
  }

  @Get(':id')
  @RequirePermission('team:read')
  findOne(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const tenantId = req.tenantId || req.user?.tenantId;
    return this.teamsService.findOne(id, tenantId);
  }

  @Patch(':id')
  @RequirePermission('team:update')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateTeamDto: UpdateTeamDto,
    @Request() req,
  ) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new BadRequestException('无法确定租户信息');
    }
    const modifierId = req.user?.userId;
    return this.teamsService.update(id, updateTeamDto, tenantId, modifierId);
  }

  @Post(':id/members')
  @RequirePermission('team:update')
  inviteMember(
    @Param('id', ParseIntPipe) teamId: number,
    @Body() inviteMemberDto: InviteMemberDto,
    @Request() req,
  ) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new BadRequestException('无法确定租户信息');
    }
    const creatorId = req.user?.userId;
    return this.teamsService.inviteMember(
      teamId,
      inviteMemberDto,
      tenantId,
      creatorId,
    );
  }

  @Delete(':id/members/:userId')
  @RequirePermission('team:update')
  removeMember(
    @Param('id', ParseIntPipe) teamId: number,
    @Param('userId', ParseIntPipe) userId: number,
    @Request() req,
  ) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new BadRequestException('无法确定租户信息');
    }
    return this.teamsService.removeMember(teamId, userId, tenantId);
  }

  @Delete(':id')
  @RequirePermission('team:delete')
  remove(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new BadRequestException('无法确定租户信息');
    }
    return this.teamsService.remove(id, tenantId);
  }
}

