import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Request,
  ParseIntPipe,
  BadRequestException,
} from '@nestjs/common';
import { ContestsService } from './contests.service';
import { CreateContestDto } from './dto/create-contest.dto';
import { UpdateContestDto } from './dto/update-contest.dto';
import { QueryContestDto } from './dto/query-contest.dto';
import { PublishContestDto } from './dto/publish-contest.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests')
@UseGuards(JwtAuthGuard)
export class ContestsController {
  constructor(private readonly contestsService: ContestsService) {}

  @Post()
  @RequirePermission('contest:create')
  create(@Body() createContestDto: CreateContestDto, @Request() req) {
    const creatorId = req.user?.userId;
    return this.contestsService.create(createContestDto, creatorId);
  }

  @Get('stats')
  @RequirePermission('contest:read')
  getStats() {
    return this.contestsService.getStats();
  }

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

  @Get('my-contests')
  @RequirePermission('contest:read', 'contest:activity:read')
  findMyContests(@Query() queryDto: QueryContestDto, @Request() req) {
    const userId = req.user?.userId;
    const tenantId = req.tenantId || req.user?.tenantId;
    if (!userId) {
      throw new BadRequestException('用户未登录');
    }
    return this.contestsService.findMyContests(queryDto, userId, tenantId, queryDto.role);
  }

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

  @Patch(':id')
  @RequirePermission('contest:update')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateContestDto: UpdateContestDto,
    @Request() req,
  ) {
    const modifierId = req.user?.userId;
    return this.contestsService.update(id, updateContestDto, modifierId);
  }

  @Patch(':id/publish')
  @RequirePermission('contest:publish')
  publish(
    @Param('id', ParseIntPipe) id: number,
    @Body() publishDto: PublishContestDto,
    @Request() req,
  ) {
    const modifierId = req.user?.userId;
    return this.contestsService.publish(id, publishDto.contestState, modifierId);
  }

  @Patch(':id/finish')
  @RequirePermission('contest:update')
  finish(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const modifierId = req.user?.userId;
    return this.contestsService.finish(id, modifierId);
  }

  @Patch(':id/reopen')
  @RequirePermission('contest:update')
  reopen(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const modifierId = req.user?.userId;
    return this.contestsService.reopen(id, modifierId);
  }

  @Delete(':id')
  @RequirePermission('contest:delete')
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.contestsService.remove(id);
  }
}

