import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Request,
  ParseIntPipe,
} from '@nestjs/common';
import { AttachmentsService } from './attachments.service';
import { CreateAttachmentDto } from './dto/create-attachment.dto';
import { UpdateAttachmentDto } from './dto/update-attachment.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';

@Controller('contests/attachments')
@UseGuards(JwtAuthGuard)
export class AttachmentsController {
  constructor(private readonly attachmentsService: AttachmentsService) {}

  @Post()
  @RequirePermission('contest:update')
  create(@Body() createAttachmentDto: CreateAttachmentDto, @Request() req) {
    const creatorId = req.user?.id;
    return this.attachmentsService.create(createAttachmentDto, creatorId);
  }

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

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

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

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

