import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Request,
  ParseIntPipe,
} from '@nestjs/common';
import { NoticesService } from './notices.service';
import { CreateNoticeDto } from './dto/create-notice.dto';
import { UpdateNoticeDto } from './dto/update-notice.dto';
import { QueryNoticeDto } from './dto/query-notice.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests/notices')
@UseGuards(JwtAuthGuard)
export class NoticesController {
  constructor(private readonly noticesService: NoticesService) {}

  @Post()
  @RequirePermission('notice:create')
  create(@Body() createNoticeDto: CreateNoticeDto, @Request() req) {
    const creatorId = req.user?.id;
    return this.noticesService.create(createNoticeDto, creatorId);
  }

  @Get('contest/:contestId')
  @RequirePermission('notice:read')
  findAll(@Param('contestId', ParseIntPipe) contestId: number) {
    return this.noticesService.findAll(contestId);
  }

  @Get()
  @RequirePermission('notice:read')
  findAllNotices(@Query() queryDto: QueryNoticeDto) {
    return this.noticesService.findAllNotices(queryDto);
  }

  @Get(':id')
  @RequirePermission('notice:read')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.noticesService.findOne(id);
  }

  @Patch(':id')
  @RequirePermission('notice:update')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateNoticeDto: UpdateNoticeDto,
    @Request() req,
  ) {
    const modifierId = req.user?.id;
    return this.noticesService.update(id, updateNoticeDto, modifierId);
  }

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

