import { Controller, HttpCode, HttpStatus, ParseFilePipe, Post, Req, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common';
import { EventUplaodService } from './event.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { ResponseUtils } from 'src/common/utils/utils.response';
import { AuthContext } from 'src/modules/auth/types/auth.types';
import { CreateAttachmentDto } from '../attachment/dto/create-attachment.dto';
import type { Request } from 'express';
import { JwtGuard } from 'src/common/guard/jwt/jwt.guard';

@Controller('upload/event')
export class EventUploadController {
    constructor(private readonly eventUploadService: EventUplaodService) { }

    @Post()
    @UseGuards(JwtGuard)
    @HttpCode(HttpStatus.CREATED)
    @UseInterceptors(FileInterceptor('file'))
    async upload(
        @Req() req: Request,
        @UploadedFile(new ParseFilePipe({ fileIsRequired: true })) file: Express.Multer.File
    ): Promise<ResponseUtils> {
        const { id } = req.user as AuthContext;
        const payload: CreateAttachmentDto = {
            userId: id
        }
        const result = await this.eventUploadService.upload(payload, file)
        return ResponseUtils.created(result, "Upload file sucessfully")
    }
}
