import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Request,
  ParseIntPipe,
} 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 Error('无法确定租户信息');
    }
    const creatorId = req.user?.id;
    return this.registrationsService.create(
      createRegistrationDto,
      tenantId,
      creatorId,
    );
  }

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

  @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?.id;
    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 Error('无法确定租户信息');
    }
    const creatorId = req.user?.id;
    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 Error('无法确定租户信息');
    }
    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);
  }
}

