import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Request,
  ParseIntPipe,
  BadRequestException,
} from '@nestjs/common';
import { RegistrationsService } from './registrations.service';
import { CreateRegistrationDto } from './dto/create-registration.dto';
import { ReviewRegistrationDto } from './dto/review-registration.dto';
import { QueryRegistrationDto } from './dto/query-registration.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests/registrations')
@UseGuards(JwtAuthGuard)
export class RegistrationsController {
  constructor(private readonly registrationsService: RegistrationsService) {}

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

  @Get('stats')
  @RequirePermission('contest:read')
  getStats(@Query('contestId') contestId?: string) {
    return this.registrationsService.getStats(contestId ? parseInt(contestId) : undefined);
  }

  @Get()
  @RequirePermission('contest:read')
  findAll(@Query() queryDto: QueryRegistrationDto, @Request() req) {
    const tenantId = req.tenantId || req.user?.tenantId;
    return this.registrationsService.findAll(queryDto, tenantId);
  }

  /**
   * 获取当前用户在某活动中的报名记录（包括作为团队成员的情况）
   */
  @Get('my/:contestId')
  @RequirePermission('contest:read')
  getMyRegistration(
    @Param('contestId', ParseIntPipe) contestId: number,
    @Request() req,
  ) {
    const userId = req.user?.userId;
    if (!userId) {
      throw new BadRequestException('用户未登录');
    }
    return this.registrationsService.getMyRegistration(contestId, userId);
  }

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

  @Patch(':id/review')
  @RequirePermission('contest:update')
  review(
    @Param('id', ParseIntPipe) id: number,
    @Body() reviewDto: ReviewRegistrationDto,
    @Request() req,
  ) {
    const tenantId = req.tenantId || req.user?.tenantId;
    const operatorId = req.user?.userId;
    return this.registrationsService.review(id, reviewDto, operatorId, tenantId);
  }

  @Post(':id/teachers')
  @RequirePermission('contest:update')
  addTeacher(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { teacherUserId: number },
    @Request() req,
  ) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new BadRequestException('无法确定租户信息');
    }
    const creatorId = req.user?.userId;
    return this.registrationsService.addTeacher(
      id,
      body.teacherUserId,
      tenantId,
      creatorId,
    );
  }

  @Delete(':id/teachers/:teacherUserId')
  @RequirePermission('contest:update')
  removeTeacher(
    @Param('id', ParseIntPipe) id: number,
    @Param('teacherUserId', ParseIntPipe) teacherUserId: number,
    @Request() req,
  ) {
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!tenantId) {
      throw new BadRequestException('无法确定租户信息');
    }
    return this.registrationsService.removeTeacher(id, teacherUserId, tenantId);
  }

  @Delete(':id')
  @RequirePermission('contest:update')
  remove(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const tenantId = req.tenantId || req.user?.tenantId;
    return this.registrationsService.remove(id, tenantId);
  }
}

